Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Xpath in c#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DBC User - 30 Aug 2006 20:43 GMT
Hi,

I have an xml and I am able to use xpath to identify each node that
statisfy the selection criteria. I got the node list. I would like to
know is it possible to do the following for the following XML
<Files>
<Application key="one">
   <Version>1</Version>
   <Age>120</Age>
</Application>
</Files>

I have lot of application and I want to find out only the ones with
'one' I got that working got the nodelist. Inside the iterator, I want
to get the values of Version and age and assign it to a class. But I do
not want to hard code something like

myClass.Version = node.ChildNodes[0].InnerText;
myClass.Age = node.ChildNodes[1].InnerText;

it works, but I would like to get it based on a element name, that
gives me the flexibility incase some adds a element in between version
or age. Or they rearrange the xml node itself.

Hope I am clear. Please let me know how can I do this in the best
possible approach? (I was thinking of applying XPath on each node to
get it, just worried the performance and resource)

Thanks.
sloan - 30 Aug 2006 20:50 GMT
Something like this. (Going from memory).

The indexer has an string argument. (see string theVersion below)

http://msdn2.microsoft.com/en-us/library/system.xml.xmlelement_members.aspx

> <Files>
>  <Application key="one">
>     <Version>1</Version>
>     <Age>120</Age>
>  </Application>
> </Files>

XmlNode nodeServer = // get a XmlNode somehow, usually the .SelectSingleNode
method

string currentXpath = "key";

    if (null!=nodeServer.Attributes[currentXpath])
    {
     string theKey  = nodeServer.Attributes[currentXpath].Value ;
    }

string currentXpath = "Version";

    if (null!=nodeServer[currentXpath])
    {
     string theVersion  = nodeServer[currentXpath].Value ;
    }

> Hi,
>
[quoted text clipped - 25 lines]
>
> Thanks.
DBC User - 30 Aug 2006 22:29 GMT
Hi Sloan,

Only problem I have is I do selectnodes not select single node so I end
up getting xmlnodelist rather than xmlnode and in xmlnodelist I can not
use key. Only available way is to use integer. So the question now is
How do I convert a xmlnodelist to xmlnode? I will do some research and
see if I can find it.

Thanks.
> Something like this. (Going from memory).
>
[quoted text clipped - 55 lines]
> >
> > Thanks.
DBC User - 30 Aug 2006 22:45 GMT
Following solution works but as I said in the first part isit right way
to do it?

XmlNode t1 = node.SelectSingleNode("Version");
string t = t1.InnerText;

> Hi Sloan,
>
[quoted text clipped - 64 lines]
> > >
> > > Thanks.
sloan - 30 Aug 2006 23:35 GMT
.SelectSingleNode is basically a short cut,,, to just get the first match.

SelectNodes gives you a list of nodes.  Usually you iterate over them if you
get a list.

  foreach (XmlNode nodeServer in nodeListServers)
  {

string currentXpath = "key";

     if (null!=nodeServer.Attributes[currentXpath])
     {
      string theKey  = nodeServer.Attributes[currentXpath].Value ;
     }

 currentXpath = "Version";

     if (null!=nodeServer[currentXpath])
     {
      string theVersion  = nodeServer[currentXpath].Value ; // NOT
.ChildNodes as you have in another post.  INDEXER.
     }

}

> Hi Sloan,
>
[quoted text clipped - 8 lines]
> >
> > The indexer has an string argument. (see string theVersion below)

http://msdn2.microsoft.com/en-us/library/system.xml.xmlelement_members.aspx

> > > <Files>
> > >  <Application key="one">
[quoted text clipped - 49 lines]
> > >
> > > Thanks.
DBC User - 31 Aug 2006 16:36 GMT
Thanks all I think I got it, it is not the childnodes, it is the
indexer.

> .SelectSingleNode is basically a short cut,,, to just get the first match.
>
[quoted text clipped - 91 lines]
> > > >
> > > > Thanks.
sloan - 31 Aug 2006 18:56 GMT
Ding Ding Ding !

Yeah, the indexer is a little tricky, esp if you come from VB.NET where it
uses the .Item(N) property.

Remember that if you code up your own indexer, you can overload it.

this[int personid]
this[string lastname]

..

> Thanks all I think I got it, it is not the childnodes, it is the
> indexer.
[quoted text clipped - 36 lines]
> > > >
> > > > The indexer has an string argument. (see string theVersion below)

http://msdn2.microsoft.com/en-us/library/system.xml.xmlelement_members.aspx

> > > > > <Files>
> > > > >  <Application key="one">
[quoted text clipped - 51 lines]
> > > > >
> > > > > Thanks.
Michael - 30 Aug 2006 21:07 GMT
* Hi,
*
* I have an xml and I am able to use xpath to identify each node that
* statisfy the selection criteria. I got the node list. I would like to
* know is it possible to do the following for the following XML
* <Files>
* <Application key="one">
*    <Version>1</Version>
*    <Age>120</Age>
* </Application>
* </Files>
*
* I have lot of application and I want to find out only the ones with
* 'one' I got that working got the nodelist. Inside the iterator, I want
* to get the values of Version and age and assign it to a class. But I do
* not want to hard code something like
*
* myClass.Version = node.ChildNodes[0].InnerText;
* myClass.Age = node.ChildNodes[1].InnerText;
*
* it works, but I would like to get it based on a element name, that
* gives me the flexibility incase some adds a element in between version
* or age. Or they rearrange the xml node itself.
*
* Hope I am clear. Please let me know how can I do this in the best
* possible approach? (I was thinking of applying XPath on each node to
* get it, just worried the performance and resource)
*
* Thanks.
*

DBC User,

The XmlNode (System.Xml.XmlNode) class' indexer does accept a string
argument for accessing child Elements.
Could you not try something like

myClass.Version = node["Version"].InnerText;
myClass.Age = node["Age"].InnerText;

--MH
,
DBC User - 30 Aug 2006 22:25 GMT
Hi Micheal,

I get a nodelist from selectnodes and then I am traversing through
XmlNode. But for some reason it looks like
for(XmlNode node in nodelist)
{
...
}
node is still in Nodelist and it takes only integer. Thats what
bothering me. Even though I did a casting it still error out as
XML.XmlNodeList. here is the code

XmlNodeList list = doc.SelectNodes(@"/Files/Application[@year=2005]");
foreach(XmlNode node in list)
{
  string t = node.ChildNodes["Version"].InnerText;
  ...
}

this fails during compile time with 'Best overload method is
System.Xml.XmlNodeList.this[int]'

Any idea??

Thanks.

> * Hi,
> *
[quoted text clipped - 38 lines]
> --MH
> ,
Michael - 30 Aug 2006 23:55 GMT
***"DBC User" <dbcuser@gmail.com> wrote in message
*** news:1156966989.660957.163870@m73g2000cwd.googlegroups.com...
*** Hi,
***
*** I have an xml and I am able to use xpath to identify each node that
*** statisfy the selection criteria. I got the node list. I would like to
*** know is it possible to do the following for the following XML
*** <Files>
*** <Application key="one">
***    <Version>1</Version>
***    <Age>120</Age>
*** </Application>
*** </Files>
***
*** I have lot of application and I want to find out only the ones with
** * 'one' I got that working got the nodelist. Inside the iterator, I want
*** to get the values of Version and age and assign it to a class. But I do
*** not want to hard code something like
** *
*** myClass.Version = node.ChildNodes[0].InnerText;
** * myClass.Age = node.ChildNodes[1].InnerText;
***
** * it works, but I would like to get it based on a element name, that
*** gives me the flexibility incase some adds a element in between version
*** or age. Or they rearrange the xml node itself.
***
*** Hope I am clear. Please let me know how can I do this in the best
*** possible approach? (I was thinking of applying XPath on each node to
*** get it, just worried the performance and resource)
** *
*** Thanks.
***
***
** DBC User,
**
** The XmlNode (System.Xml.XmlNode) class' indexer does accept a string
** argument for accessing child Elements.
** Could you not try something like
**
** myClass.Version = node["Version"].InnerText;
** myClass.Age = node["Age"].InnerText;
**
**
** --MH
** ,
**
* Hi Micheal,
*
* I get a nodelist from selectnodes and then I am traversing through
* XmlNode. But for some reason it looks like
* for(XmlNode node in nodelist)
* {
* ...
* }
* node is still in Nodelist and it takes only integer. Thats what
* bothering me. Even though I did a casting it still error out as
* XML.XmlNodeList. here is the code
*
* XmlNodeList list = doc.SelectNodes(@"/Files/Application[@year=2005]");
* foreach(XmlNode node in list)
* {
*   string t = node.ChildNodes["Version"].InnerText;
*   ...
* }
*
* this fails during compile time with 'Best overload method is
* System.Xml.XmlNodeList.this[int]'
*
* Any idea??
*
* Thanks.
*

DBC User,

Within your foreach loop structure, node is of type System.Xml.XmlNode
so calling the ChildNotes property returns an System.Xml.XmlNodeList as
you've mentioned.
We don't want an XmlNodeList here if we're to try using the XmlNode class'
indexer.
We want an XmlNode which is what your loop scope variable "node" is.
So, You may try altering the line

string t = node.ChildNodes["Version"].InnerText;

into this

string t = node["Version"].InnerText;

-MH

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.