您的位置:首页 > 其它

应用程序写Xml文档

2016-04-15 00:13 337 查看
主要用到CreateElement、CreateTextNode、CreateComment、AppendChild、InsertAfter方法

代码如下:

XmlDocument document = new XmlDocument();
document.Load("XmlReader.xml");
//Get the root element
XmlElement element = document.DocumentElement;
//Create new nodes
XmlElement newbook = document.CreateElement("book");
XmlElement newtitle = document.CreateElement("title");
XmlElement newauthor = document.CreateElement("author");
XmlElement newcode = document.CreateElement("code");
XmlText title = document.CreateTextNode("C# From fresh to junior");
XmlText author = document.CreateTextNode("simith");
XmlText code = document.CreateTextNode("c# begin");
XmlComment comment = document.CreateComment("append publish");

//insert into xml document
newbook.AppendChild(newtitle);
newbook.AppendChild(newauthor);
newbook.AppendChild(newcode);
newtitle.AppendChild(title);
newauthor.AppendChild(author);
newcode.AppendChild(code);
element.InsertAfter(newbook, element.FirstChild);
document.Save("XmlReader.xml");
Console.ReadKey();


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: