Altova Mailing List Archives


Re: XPath Query in ASP.NET

From: "Han" <hp4444@------.---.----->
To: NULL
Date: 1/19/2005 12:46:00 PM
Add one or more predicates [] and some conditions as you want.

XmlNode n=doc.SelectSingleNode("//ns:tblItem[ns:ID=1]", nsMgr);
or
XmlNode n=doc.SelectSingleNode("//ns:tblItem[ns:Description='Remote']", 
nsMgr);
or
XmlNode n=doc.SelectSingleNode("//ns:tblItem[ns:ID=1 or 
ns:Description='Remote']", nsMgr);
or
foreach (XmlNode n in doc.SelectNodes("//ns:tblItem[ns:ID=1]", nsMgr)) {...}
or

-- 
Pohwan Han. Seoul. Have a nice day.
"Michael C#" <xyz@a...> wrote in message 
news:XgjHd.4545$rn3.1323@f......
> Cool, that seems to work.  Now how do I add criteria to it.  For instance, 
> I want only the tblItem nodes that contain an ID node with a value of '1' 
> or tblItem nodes with a Description node that has 'Remote' in it, in any 
> position?
>
> Thanks for the help
>
>
> "Han" <hp4444@k...> wrote in message 
> news:eCrW3ST$EHA.2552@T......
>> You need to first make the xml well-formed.
>> Next, need to set a namespace. Something like,
>>
>> XmlDocument doc=new XmlDocument();
>> doc.Load("c:/x.xml");
>> XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
>> nsMgr.AddNamespace("ns", "http://www.tempuri.org/DataSet1.xsd");
>> XmlNode n=doc.SelectSingleNode("//ns:tblItem", nsMgr);
>>
>> If you still have problems, let us know that please.
>> -- 
>> Pohwan Han. Seoul. Have a nice day.
>> "Michael C#" <xyz@a...> wrote in message 
>> news:f80Hd.4454$lv5.530@f......
>>> OK, here's the deal.  I have a small XML file that represents a small
>>> database table.  I load it into a System.XML.XMLDocument.  So far so 
>>> good.
>>> I run an XPath query against it to retrieve all the field names. 
>>> Everything
>>> there works fine.
>>>
>>> Here's my XML Document:
>>>
>>> <?xml version="1.0" standalone="yes" ?>
>>> <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
>>>  <tblItem>
>>>    <ID>1</ID>
>>>    <Name>Spam</Name>
>>>    <Category>Food</Category>
>>>    <Description>Yummy! No natural ingredients</Description>
>>>    <Price>4</Price>
>>>    <ImageURL>images/1.png</ImageURL>
>>>    <LargeImageURL>images/L1.png</LargeImageURL>
>>>  </tblItem>
>>>  <tblItem>
>>>    <ID>2</ID>
>>>    <Name>Remote Control</Name>
>>>    <Category>Miscellaneous</Category>
>>>    <Description>Universal Remote</Description>
>>>    <Price>12</Price>
>>>    <ImageURL>images/2.png</ImageURL>
>>>    <LargeImageURL>images/L2.png</LargeImageURL>
>>>  </tblItem>
>>> </DataSet>
>>>
>>> Now for the tricky part.  I'm trying to come up with three XPath queries
>>> that will return the following:
>>>
>>> 1) All tblItem nodes that have a child ID node with a value of 1 (i.e., 
>>> all
>>> tblItem where ID = 1),
>>> 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
>>> tblItem where Category = 'Miscellaneous')
>>> 3) All tblItem ndoes that have the word Remote in the Description (i.e., 
>>> all
>>> tblItem where Category contains the word 'Remote' in any position)
>>>
>>> Coming from a SQL background, I'm having a hard time implementing XPath
>>> expressions.  I was hoping someone here could point me in the right
>>> direction.  I've tried several combinations, like "tblItem/ID[.='1']",
>>> "tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']".  None of them 
>>> seem to
>>> be working though...
>>>
>>> TIA
>>>
>>>
>>>
>>
>
> 

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.