Xpath in c#

D

DBC User

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.
 
S

sloan

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 ;
}


DBC User said:
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.
 
M

Michael

* 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
,
 
D

DBC User

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.
 
D

DBC User

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.
sloan said:
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 ;
}


DBC User said:
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.
 
D

DBC User

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;

DBC said:
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.
sloan said:
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 ;
}


DBC User said:
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.
 
S

sloan

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

}



DBC User said:
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.
sloan said:
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 ;
}


DBC User said:
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.
 
M

Michael

*** *** 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
 
D

DBC User

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.

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

}



DBC User said:
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.
sloan said:
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,

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.
 
S

sloan

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]

...




DBC User said:
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.

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

}



DBC User said:
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.
sloan wrote:
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,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top