一下介绍xml的基本操作,添加xml新节点;
其他方法在前一篇日记中有详细讲解,请详见:http://www.cnblogs.com/fjsnail/archive/2012/10/20/2732127.html
public void addElementText()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("../../testXml1.xml");
//get the root element
XmlElement root = xmldoc.DocumentElement;
//create new nodes
XmlElement newbook=xmldoc.CreateElement("book");
XmlElement newTitle = xmldoc.CreateElement("Title");
XmlElement newAuthor = xmldoc.CreateElement("author");
XmlElement newCode = xmldoc.CreateElement("code");
XmlText titleText = xmldoc.CreateTextNode("how to learn C#2");
XmlText AuthorText = xmldoc.CreateTextNode("xianyun");
XmlText codeText = xmldoc.CreateTextNode("3543453");
XmlComment comment = xmldoc.CreateComment("nimeimeiede");
XmlAttribute attrubute = xmldoc.CreateAttribute("ha");
这里,我觉得用之前那种方法更简单
newTitle.innerText(“nihao”);
//insert the elements
newbook.AppendChild(newTitle);
newbook.AppendChild(newAuthor);
newbook.AppendChild(newCode);
newbook.AppendChild(comment);
newTitle.AppendChild(titleText);
newTitle.AppendChild(attrubute);
newAuthor.AppendChild(AuthorText);
newCode.AppendChild(codeText);
root.InsertAfter(newbook, root.LastChild);
xmldoc.Save("../../testXml1.xml");
}