在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>段,然后修改完写回去,其他的段不变.
<?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>段,然后修改完写回去,其他的段不变.
解决方案 »
- 【服务器上创建文件夹 受限制 求解决方法】
- C# winform如何实现下载到缓存解压缩
- C#如何使用Lable控件输出数组
- 急,急急,在线等!! VCS express 调试不了,如何解决?
- 高手青帮忙啊!万分感谢
- 请教---排序问题~~谢谢
- 如何才能把当前屏幕的内容保存到位图中?
- imagebutton没有enable属性吗?除了写程序让它失效,能否让它看得见,但点了没有效。
- winform中, 怎么在picturebox中画一条直线
- 关于WinForm中TextBox的TextChanged事件
- 请问重装PetShop 3.0后运行起来怎么报错啊?谢谢!
- 关于GDI+绘制不规则窗体(圆角矩形)的问题。
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");
}
不过还有个更好的,
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;//读:
反过来就可以了
myDoc.Save(xmlfile);
old_dustman()的方法虽然可行,但是代码复用不好。