C#读写xml文件. 2

此文来源:http://kb.cnblogs.com/page/42226/?page=2
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。

Code
最后结果为:
Code
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据。

XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl
=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe
=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute(
"genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));

XmlNodeList xnf1
=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);
//显示子节点点文本
}
}

 

posted @ 2009-07-30 12:58  大明1986  阅读(264)  评论(0编辑  收藏  举报