2013-01-08 163 views
1

我的XML文件是: 將數據添加到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> 

我如何可以添加到這個現有的文件,像新的紀錄:

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

或刪除或者如果你知道那麼如何更新現有的記錄呢。謝謝

+1

請問您可以指定「或刪除或者如果您知道如何更新現有記錄,那麼這也是'是什麼意思? –

+0

查看有關XmlWriter和XDocument的MSDN文章:http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(v=vs.100).aspx和http:// msdn。 microsoft.com/en-us/library/bb336977(v=vs.95).aspx - >這裏有許多方法的代碼示例。 – Ray

回答

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> 
+0

但是你的代碼給了我嚴格歸因的str變量。你知道如何附加到str變量整個XML文檔? –

+0

@ŁukaszWróblewski你的意思是我已經在'str'變量中打印了xml嗎? –

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值的值。

+0

doc.root.element(...)您的意思是xdoc.root.element? –

+0

您的代碼無法以任何方式工作。有大約20個錯誤。你確定它適用於wp7嗎? –