ASP.NET 2.0:如何使用XPath?

问题描述:

如果我们可以在ASP.NET 2.0中的XPath通过获得的x个节点集合?然后按照他们的意见进行检查。ASP.NET 2.0:如何使用XPath?

<x-list> 
    <x id="1" enable="On" url="http://abc.123.dev"/> 
    <x id="2" enable="Off" url="http://asd.com"/> 
    <x id="3" enable="On" url="http://plm.xcv.tw"/> 
</x-list> 

感谢您的任何帮助。 瑞奇

+0

我只是想知道为什么enable属性不是布尔字段 – Shimmy 2009-11-23 02:30:34

+0

另外,你使用什么语言,C#或VB? – Shimmy 2009-11-23 02:37:16

+0

考虑,如果你已经使用了更高版本的.NET与VB你可以享受VB XML文本的东西:http://ookii.org/post/xml_literals_in_visual_basic_9.aspx – Shimmy 2009-11-23 02:38:59

下面是会检索所有的“X”节点的样本已启用:

XmlNodeList nodes = root.SelectNodes("/x-list/x[@enable='On']"); 
foreach (XmlNode node in nodes) 
{ 
    ... 
} 

我发现W3Schools的一个很好的地方去寻找XPath tutorials

+0

另外:http://oreilly.com/catalog/xmlnut/chapter/ch09.html和(使用PERL)http://oreilly.com/perl/excerpts/system-admin-with-perl/ten-minute-xpath -utorial.html – 2009-11-23 03:00:00

+0

另一个问题:如何将“AND”应用于属性选择器 – Ricky 2009-11-23 08:53:48

+0

并且您是否知道如何使XPath查询大小写“in”敏感? – Ricky 2009-11-23 09:06:25

XmlDocument xDocument = new XmlDocument(); 
xDocument.LoadXml(xmlString); 

XmlNodeList xList = xDocument.SelectNodes("x-list/x"); 

if (xList.Count > 0) 
{ 
foreach (XmlNode x in xList) 
    { 
     string id = x.Attributes["id"].Value; 
    string enable = x.Attributes["enable"].Value; 
    string url = x.Attributes["url"].Value; 

    if (enable.Equals("On") 
    { 
    ... 
    } 
    else 
    { 
    ... 
    } 

} 
} 
else 
{ 
... 
}