您的位置:首页 > Web前端 > Node.js

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.Selec­tNodes to get list of nodes selected by the XPath expression. Suppose we have this XML file.

[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: