更新xml中的记录

时间:2012-08-11 21:08:53

标签: c# xml linq-to-xml

通过这种方式,我可以获取记录的详细信息,我想知道如何编辑值并将其更新为同一记录?

如何检查属性中的唯一值?

XDocument doc = XDocument.Load(filePath);

string id = textBox6.Text;
XElement element = doc.Descendants("Customer").FirstOrDefault(p => p.Attribute("id").Value == id);

if (element != null)
{
    //found
    textBox6.Text = textBox6.Text;
    textBox1.Text = (string)element.Element("FirstName");
    textBox2.Text = (string)element.Element("LastName"); 
    textBox3.Text = (string)element.Element("Mobile");
    textBox4.Text = (string)element.Element("Address");
    textBox5.Text = (string)element.Element("Country");
}

示例XML:

<?xml version="1.0" encoding="utf-8"?>
<CustomerRecord>
  <Customer id="43543543">
    <FirstName>sdf</FirstName>
    <LastName>fdsf</LastName>
    <Mobile>23452345é</Mobile>
    <Address>zfdsf</Address>
    <Country>dsfdsf</Country>
  </Customer>
</CustomerRecord>

3 个答案:

答案 0 :(得分:1)

XElement element;
XDocument doc;

private void Load()
{
    doc = XDocument.Load(filePath);

    string id = textBox6.Text;
    element = doc.Descendants("Customer").FirstOrDefault(p => p.Attribute("id").Value == id);

    if (element != null)
    {
        //found
        textBox6.Text = textBox6.Text;
        textBox1.Text = (string)element.Element("FirstName");
        textBox2.Text = (string)element.Element("LastName"); 
        textBox3.Text = (string)element.Element("Mobile");
        textBox4.Text = (string)element.Element("Address");
        textBox5.Text = (string)element.Element("Country");
    }
}

protected void btnSave_Click(object sender, EventArgs e)
{
    element.Attribute("id").Value = textBoxID.text;
    var firstNameSimilarElement = doc.Descendants("FirstName").FirstOrDefault(fn => fn.Value == textBox1.Text)
    if (firstNameSimilarElement != null && firstNameSimilarElement.Parent != element)
    {
         MessageBox.Show("First Name already exists");
    }
    else
    {
        element.Element("FirstName").Value = textBox1.Text;
    }
    // Same for all fields
    doc.Save(filePath);
}

答案 1 :(得分:1)

例如,我只使用2个子节点(FirstName,LastName)。我把file.xml和exe文件放在同一目录下,这是代码:

using System;
using System.Windows.Forms;
using System.Xml;

namespace UpdateXmlRecord
{
    public partial class Form1 : Form
    {
        private XmlDocument xDoc;

        public Form1()
        {
            InitializeComponent();
            xDoc = new XmlDocument();
            xDoc.Load("file.xml");
        }

        /// <summary>
        /// Retrieve Xml Node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string id = txtId.Text;
            string fName = "", lName = "";
            foreach (XmlElement element in xDoc.DocumentElement)
            {
                if (element.Name == "Customer" && element.Attributes["id"].Value == id)
                {
                    foreach (XmlNode node in element)
                    {
                        if (node.Name == "FirstName")
                        {
                            fName = node.InnerText;
                        }

                        if (node.Name == "LastName")
                        {
                            lName = node.InnerText;
                        }
                    }
                }
            }

            txtFName.Text = fName;
            txtLName.Text = lName;
        }

        /// <summary>
        /// Save Xml Node With Custom Value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string id = txtId.Text;
            foreach (XmlElement element in xDoc.DocumentElement)
            {
                if (element.Name == "Customer" && element.Attributes["id"].Value == id)
                {
                    foreach (XmlNode node in element)
                    {
                        if (node.Name == "FirstName")
                        {
                            node.InnerText = txtFName.Text;
                        }

                        if (node.Name == "LastName")
                        {
                            node.InnerText = txtLName.Text;
                        }
                    }
                }
            }
            xDoc.Save("file.xml");
        }
    }
}

希望它有所帮助,享受它。 :)

答案 2 :(得分:0)

您可以对节点的内容进行哈希处理,并将哈希附加到生成的XML中。哈希将足够独特,充当您记录的“主键”。