C#操作XML文档:我用C# WinForm读取XML文档,读取可以正常显示,可是我想修改指定的节点的值如何写代码?
我这样一段代码,看一下:<?xml version="1.0" encoding="GB2312"?>
<school version="2.0">
  <students>
     <class>32班</class>
<item>
  <name>李四</name>
  <sex>男</sex>
</item>
<item>
  <name>张小娴</name>
  <sex>女</sex>
</item>
  </students>
  <students1>
     <class>33班</class>
<item>
  <name>王全</name>
  <sex>男</sex>
</item>
<item>
  <name>刘晓燕</name>
  <sex>女</sex>
</item>
  </students1>
</school>我想修改的是“李四”这个名字,如何实现?
还有,再再student1节点中添加一个项:
         <item>
  <name>刘小明</name>
  <sex>男</sex>
</item>删除呢?比如我想删除其中的一项,如何操作啊?给点代码研究研究哈。谢谢了

解决方案 »

  1.   

    参考一下
    http://blog.csdn.net/lovefootball/archive/2008/08/21/2785922.aspx
      

  2.   

    doc.Load("books.xml");
    XmlElement newBook=doc.CreateElement("book");
    newBook.SetAttribute("genre","Mystery");
    newBook.SetAttribute("publicationdate","2001");
    newBook.SetAttribute("ISBN","123456789");
    //
    XmlElement newTitle=doc.CreateElement("title");
    newTitle.InnerText="Case of the Missing Cookie";
    newBook.AppendChild(newTitle);
      

  3.   

    XmlDocument xmldocument = xmldocument.Load(filename);
    foreach (XmlNode node in xmldocument.ChildNodes)   

    node.InnerText=修改的值             //node是你要修改的节点
    }