在C#中如何实现读XML文件中的一段,将其修改后并写回去.比如:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>我只要读出这个文件中的第一个<book></book>段,然后修改完写回去,其他的段不变.

解决方案 »

  1.   

    System.Xml.XmlDocument xd=new System.Xml.XmlDocument();
    xd.Load("1.xml");
    System.Xml.XmlNode xn=xd.DocumentElement.SelectSingleNode("book");
    if(xn!=null)
    {
    xn.Attributes["genre"].Value=xn.Attributes["genre"].Value.ToUpper();
    xd.Save("1.xml");
    }
      

  2.   

    那如果是想得到<title>的值进行修改该如何.
      

  3.   

    其实 chenyuming2004(这辈子我算是废了) 的方法也不错,
    不过还有个更好的,
    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;string xmlfile="c:/spy.xml";
    XmlDocument myDoc=new XmlDocument();
    //写:
    myDoc.ChildNodes.Item(1).ChildNodes.Item(0).ChildNodes.Item(0).InnerText=this.textBox1.Text;//读:
    反过来就可以了
      

  4.   

    差点忘了,写完要保存一下,
     myDoc.Save(xmlfile);
      

  5.   

    提倡chenyuming2004(这辈子我算是废了)的方法,
    old_dustman()的方法虽然可行,但是代码复用不好。