2015-04-23 68 views
1

我得到這個功能只是得到我的XML的內文:XML - 寫單個節點

XmlDocument document = new XmlDocument(); 
document.Load("game.xml"); 
string content = document.SelectSingleNode("Game/Client-Version").InnerText; 

(這是XML文件(由於與計算器併發症張貼在引擎收錄)):http://pastebin.com/EEeFAJpC

現在我正在尋找上面的功能,只是寫。像

document.WriteSingleNode("Game/Client-Version", "texttowrite"); 

我沒有找到任何幫助我的東西。

回答

0

這應該工作

XmlElement x = document.SelectSingleNode("Game/Client-Version") as XmlElement; 
x.InnerText = "texttowrite"; 
+0

謝謝!我也發現了另一種方式 - 'xDocument = XDocument.Load(xmlPath); xDocument.Descendants(「Language」)。First()。Value = language; xDocument.Save(xmlPath);' –

0

創建自己的擴展方法:

public void WriteSingleNode(this XmlDocument document, string NodeName, string InnerText) 
     { 

      // Create a new element node. 
      XmlNode newElem = document.CreateNode("element", "pages", ""); 
      newElem.InnerText = InnerText; 

      Console.WriteLine("Add the new element to the document..."); 
      document.DocumentElement.AppendChild(newElem); 

      Console.WriteLine("Display the modified XML document..."); 
      Console.WriteLine(document.OuterXml); 
     }