分享

C#读写xml文件(2)

 maoj66 2013-09-04

C#读写xml文件

作者: nileel  来源: CSDN  发布时间: 2008-09-09 19:13  阅读: 62487 次  推荐: 17   原文链接   [收藏]  
[1] C#读写xml文件
[2] C#读写xml文件
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
Code
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe
=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute(
"genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();
//删除该节点的全部内容
}
}
xmlDoc.Save(
"bookstore.xml");
最后结果为:
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);
//显示子节点点文本
}
}

[第1页][第2页]

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多