tags:

views:

96

answers:

3

Hello guys:

If we can get a collection of x nodes via XPath in ASP.NET 2.0? Then doing checking per the attrs of them.

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

Thanks for any help. Ricky

+1  A: 

I would recomment you using the XML Web Server Control.

You might find this link helpful as well.

Shimmy
thanks, but i need to impl it in code-behind
Ricky
I guess u can use Michel Petrotta's, he did it simply nice, besides I've already forgot how to do it, I use LINQ 2 XML. Recommended (if you don't use it yet...).
Shimmy
+3  A: 

Here's a sample that'll retrieve all of your 'x' nodes that are enabled:

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

I find w3schools a good place to look for XPath tutorials.

Michael Petrotta
Also: http://oreilly.com/catalog/xmlnut/chapter/ch09.html and (using PERL) http://oreilly.com/perl/excerpts/system-admin-with-perl/ten-minute-xpath-utorial.html
Jim Schubert
Another question: how to apply "AND" to attributes selectors
Ricky
And do you know how to make the XPath query case-"in"sensitive?
Ricky
XPath is case-sensitive. You'd be better off enforcing correct case in your documents. If you absolutely have to do case-insensitive queries, look into the translate() function: http://shujaatsiddiqi.blogspot.com/2009/02/xpath-for-case-insensitive-search.html
Michael Petrotta
Look to the XPath tutorial (specifically, the page describing operators) for information on its boolean operators.http://www.w3schools.com/XPath/xpath_operators.asp
Michael Petrotta
+1  A: 
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
{
...
}
Rashmi Pandit