Adding data to XML file

南笙酒味 提交于 2019-12-11 06:46:51

问题


My XML file is:

<?xml version="1.0" encoding="utf-8" ?>
<people>
<person
index="1"
name="Zlecenie numer jeden"
beneficiary="Kowalski"
description="Proste zlecenie jakiejs strony czy cos"
price="800"
deadline="27.12.2013" />
</people>

How can I add to this existing file, something like new record:

<person
index="4"
name="Zlecenie numer cztery"
beneficiary="Kowalski"
description="Proste zlecenie jakiejs strony czy cos"
price="800"
deadline="27.12.2013" />

or remove or if you know how to update existing record then this too. Thanks


回答1:


Try the next code snippet for adding element into xml. Note that I've used xml as string with escaped characters. You probably have xml file

var str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<people>\r\n<person\r\nindex=\"1\"\r\nname=\"Zlec" +
"enie numer jeden\"\r\nbeneficiary=\"Kowalski\"\r\ndescription=\"Proste zlecenie jakiejs " +
"strony czy cos\"\r\nprice=\"800\"\r\ndeadline=\"27.12.2013\" />\r\n</people>";

var xml = XElement.Parse(str);
var newNode = new XElement("person", 
                    new XAttribute("index", 4),
                    new XAttribute("name", "Zlecenie numer cztery"),
                    new XAttribute("beneficiary", "Kowalski"),
                    new XAttribute("description", "Proste zlecenie jakiejs strony czy cos"),
                    new XAttribute("price", 800),
                    new XAttribute("deadline", "27.12.2013"));

xml.Add(newNode);

//you can store whole xml tree in one variable simply by calling ToString on xml
str = xml.Tostring();

Console.WriteLine(str);

Prints:

<people>
  <person index="1" name="Zlecenie numer jeden" beneficiary="Kowalski" description="Proste zlecenie jakiejs strony czy cos" price="800" deadline="27.12.2013" />
  <person index="4" name="Zlecenie numer cztery" beneficiary="Kowalski" description="Proste zlecenie jakiejs strony czy cos" price="800" deadline="27.12.2013" />
</people>



回答2:


XDocument xdoc = XDocument.Load(xmlFileName);

void Update(XDocument xdoc )
{
     XElement repser = doc.Root.Element("people").Elements("person").Where(r => (int)r.Attribute("index") = xdoc.index).FirstOrDefault();
    if (repser != null)
    {
        // update
        repser.SetAttribute("name", xdoc.name);
        repser.SetAttribute("beneficiary", xdoc.beneficiary);
        repser.SetAttribute("description", xdoc.description);
        repser.SetAttribute("price", xdoc.price);
        repser.SetAttribute("deadline", xdoc.deadline);
        // and so on
     }
     else
     {
        //insert
        doc.Root.Element("people").Add
           new XElement("person",
             new XAttribute("index", xdoc.id),
             new XAttribute("name", xdoc.name),
             new XAttribute("beneficiary", xdoc.beneficiary),
             new XAttribute("description", xdoc.description),
             new XAttribute("price", xdoc.price),
             new XAttribute("deadline", xdoc.deadline)
             // and so on
           ));
      }
}

You can manually plug in the values in the XAttribute values in the else statement for the update.



来源:https://stackoverflow.com/questions/14219138/adding-data-to-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!