471,066 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Xpath in c#

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.

Aug 30 '06 #1
9 3500
Something like this. (Going from memory).

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

http://msdn2.microsoft.com/en-us/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.

Aug 30 '06 #2
*"DBC User" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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
,
Aug 30 '06 #3
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.

Michael wrote:
*"DBC User" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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
,
Aug 30 '06 #4
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/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.
Aug 30 '06 #5
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 User wrote:
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/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.
>
Aug 30 '06 #6

..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" <db*****@gmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
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/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.
>

Aug 30 '06 #7
***"DBC User" <db*****@gmail.comwrote in message
*** news:11**********************@m73g2000cwd.googlegr oups.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
** ,
**
*"DBC User" <db*****@gmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
* 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


Aug 30 '06 #8
Thanks all I think I got it, it is not the childnodes, it is the
indexer.

sloan wrote:
.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" <db*****@gmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
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/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.
Aug 31 '06 #9

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" <db*****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Thanks all I think I got it, it is not the childnodes, it is the
indexer.

sloan wrote:
.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" <db*****@gmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
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/lib...t_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" <db*****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.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.
>
>

Aug 31 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
4 posts views Thread by Son KwonNam | last post: by
5 posts views Thread by laks | last post: by
9 posts views Thread by David Thielen | last post: by
5 posts views Thread by Gnic | last post: by
3 posts views Thread by Jason Mobarak | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.