Select XML Nodes by Attribute Value [C#]
2012-04-07 16:29
375 查看
This example shows how to select nodes from XML document by attribute value. Use methodXmlNode.SelectNodes to get list of nodes selected by the XPath expression. Suppose we have this XML file.
[XML]
To get all name nodes use XPath expression
[C#]
The output is:
[XML]
<Names> <Name type="M">John</Name> <Name type="F">Susan</Name> <Name type="M">David</Name> </Names>
To get all name nodes use XPath expression
/Names/Name. To get only male names (to select all nodes with specific XML attribute) use XPath expression
/Names/Name[@type='M'].
[C#]
XmlDocument xml = new XmlDocument(); xml.LoadXml(str); // suppose that str string contains "<Names>...</Names>" XmlNodeList xnList = xml.SelectNodes("/Names/Name[@type='M']"); foreach (XmlNode xn in xnList) { Console.WriteLine(xn.InnerText); }
The output is:
John David
相关文章推荐
- Select XML Nodes by Name [C#]
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
- C# XML SelectNodes/SelectSingleNode
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
- c# xml XPath SelectNodes, SelectSingleNode 无法获取
- C#操作xml SelectNodes,SelectSingleNode xmlns 总是返回NULL 与 xPath 介绍
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- 黄聪:C#操作xml SelectNodes,SelectSingleNode通过 xPath 定位class包含Contains的DIV
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- sql server XML中value(),exists(),nodes()方法
- XmlNode.SelectNodes 方法 (String)
- 【转】为 XmlNode.SelectNodes 加上排序功能
- Sort c# Dictionary by Value
- c# setxmlvalue
- [JavaScript] 兼容IE、FireFox、Chrome等浏览器的xml处理函数(xml同步/异步加载、xsl转换、selectSingleNode、selectNodes)
- xmlDoc.SelectNodes用法(获取不到节点时注意事项)