1

私のXMLファイルは次のとおりです。

<?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" />

または削除するか、既存のレコードを更新する方法を知っている場合は、これも。ありがとう

4

2 に答える 2

0

要素を xml に追加するための次のコード スニペットを試してください。エスケープ文字を含む文字列として xml を使用したことに注意してください。あなたはおそらくxmlファイルを持っています

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);

版画:

<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>
于 2013-01-08T16:12:11.310 に答える
0
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
           ));
      }
}

更新のelseステートメントのXAttribute値に手動で値を組み込むことができます。

于 2013-01-08T16:24:26.097 に答える