简体   繁体   English

XML-编写单个节点

[英]XML - Write a single node

I got this function for simply get the inner text of my xml: 我得到此函数只是为了获取xml的内部文本:

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

(this is the xml file (due to complications with stackoverflow posted on pastebin)): http://pastebin.com/EEeFAJpC (这是xml文件(由于在pastebin上发布stackoverflow的复杂性)): http : //pastebin.com/EEeFAJpC

And now I am exactly looking for the function above, just to write. 现在,我恰好在寻找上面的功能,仅用于编写。 Like 喜欢

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

I did not find anything helping me out. 我没有找到任何帮助我的东西。

This should work 这应该工作

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

Create your own extension method: 创建您自己的扩展方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM