C#中用XmlDocument对象获取带名命空间XML文件的节点值
来源:互联网 发布:网络gif保存到电脑 编辑:程序博客网 时间:2024/04/29 14:57
xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.abcd.com">
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.98</price>
</book>
<bookstore>
<title lang="eng">Learning C#</title>
<price>55.23</price>
</bookstore>
<item name="1">第一个item</item>
<item name="2">
<item name="1">这个结点(1)</item>
<item name="2">这个结点(2)</item>
<book>
<title lang="cn">Learning C</title>
<price>60.95</price>
</book>
</item>
</bookstore>
cs操作代码:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("books.xml"));
XmlNamespaceManager nm = new XmlNamespaceManager(xdoc.NameTable);
nm.AddNamespace("NM", "http://www.abcd.com");
XmlNodeList xnl = xdoc.SelectNodes("/NM:bookstore/NM:book", nm);
string nodeTempStr = "";
foreach (XmlNode node in xnl)
{
nodeTempStr = node.InnerText;
nodeTempStr = node.SelectSingleNode("NM:title", nm).InnerText;
nodeTempStr = node.ChildNodes[0].InnerText;
}
XmlNodeList xnodel = xdoc.SelectNodes("/NM:bookstore/NM:item[@name='2']/NM:item", nm);
XmlNodeList xnodel4 = xdoc.SelectNodes("/NM:bookstore/NM:item[.//*[@name='2']]/NM:item", nm); // 其中.//*表示当前节点下的所有元素
XmlNode xnode = xdoc.SelectSingleNode("/NM:bookstore/NM:item[@name='2']/NM:item[@name='2']", nm);
XmlNodeList xnode1 = xdoc.SelectNodes("/NM:bookstore/NM:book[NM:price>31]", nm); //选取所有 bookstore 元素的 book 元素,且其中的 price 元素的值须大于 31。
XmlNodeList xnode2 = xdoc.SelectNodes("/NM:bookstore/NM:book[NM:price>31]/NM:title", nm); //选取所有 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值须大于 31
XmlNodeList xnode3 = xdoc.SelectNodes("/NM:bookstore/NM:item[@name]", nm); //选取所有拥有名为 name 的属性的 item 元素
string attValue = xnode.Attributes["name"].Value; //先获取节点,然后再取得该节点中的属性值
string nodeText = xnode.ChildNodes[0].InnerText;
- C#中用XmlDocument对象获取带名命空间XML文件的节点值
- C#中用XmlDocument对象获取XML文件中的节点值
- C#中用Treeview空间显示XML文件
- C# 读取xml中特定节点的属性值 xmldocument方式
- C# 读取xml中特定节点的属性值 xmldocument方式 【有些乱,但有思路】
- 通过XPathDocument、XmlDocument读取Xml文件中的节点值
- C#中用XmlTextReader对象操作XML文件
- C# XML文件的一些操作(XmlDocument、XmlElement、XmlNode)
- C# XML文件的一些操作(XmlDocument、XmlElement、XmlNode)
- 获取XML文件某一个节点的值
- c#获取以及新增带命名空间的xml节点小例子
- c# 获取xml文件中某些节点下的标签节点
- C# XML XmlDocument
- C# XML XmlDocument
- 用XmlDocument的处理XML文件
- C#解析xml(获取指定节点值)
- boost 解析XML文件,获取子节点的属性值
- XML(4)XDocument和XmlDocument搜索指定的节点
- 软件安装时Error 1335 - cannot copy错误的解决方法
- python技巧(3)——下划线,私有变量
- 在VS2008下调试OpenGL程序经验
- 最熟悉的陌生人
- 获取远端Windows2000服务器系统信息
- C#中用XmlDocument对象获取带名命空间XML文件的节点值
- 保护视力配色方案
- 写给自己的信
- 108句让人欲语还休的经典话语
- C#中用XmlTextReader对象操作XML文件
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
- RAII惯用法:C++资源管理的利器
- ARM Linux静态映射分析
- JavaScript验证上传文件格式及大小