应用程序写Xml文档
主要用到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();
作者:Simen.Net
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号