2009-11-23 46 views
0

如果我們可以在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

回答

3

下面是會檢索所有的「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

1
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 
{ 
... 
}