XML Help

  • Thread starter Thread starter baggy
  • Start date Start date
B

baggy

Hi,

I need help retrieving the value of node.
My XML looks like:-

origXML = "<gb><customerid type = '1'></customerid><customerid type =
'2'>{EC96CDE1-9C58-4D03-8683-6C03F6C5A404}</customerid><modifiedby>{659BEE75-D47E-4394-872B-EFB5A8D3E055}</modifiedby><modifiedon>2005-08-11T01:22:10Z</modifiedon></gb>";

I have tried this amongst other things and can't get it:-


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = origXML;
XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid");
string strCustomerId = xmlNode.InnerText.ToString();

Hope somebody can help

thanks in advance,
baggy
 
Hi Baggy,
I am not sure which node you are trying to get, but the code you have used:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = origXML;
XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid");
string strCustomerId = xmlNode.InnerText.ToString();

in this the XPath matches multiple nodes in your xml, infact all
<customerid> nodes, so it will return the first one it finds, in your case
this will be :

<customerid type = '1'></customerid>

Then you try to get the inner text, which is perfectly valid, but it doesn't
have anything so it will return an empty string.

You need to define your Xpath a bit more, to make it more precise, I am
guessing you are trying to get the session guid so you would want to say
something like:

XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid[@type=2]");

This will select the customerid nodes but only the one where the type
attribute equals 2. Again if you have more than one node matching this then
the SelectSingleNode will return to you the first one it finds.

Hope this helps
Mark R Dawson.
 
Thanks alot Mark. That was exactly what I was looking.

Thanks again
Baggy

Hi Baggy,
I am not sure which node you are trying to get, but the code you have used:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = origXML;
XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid");
string strCustomerId = xmlNode.InnerText.ToString();

in this the XPath matches multiple nodes in your xml, infact all
<customerid> nodes, so it will return the first one it finds, in your case
this will be :

<customerid type = '1'></customerid>

Then you try to get the inner text, which is perfectly valid, but it doesn't
have anything so it will return an empty string.

You need to define your Xpath a bit more, to make it more precise, I am
guessing you are trying to get the session guid so you would want to say
something like:

XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid[@type=2]");

This will select the customerid nodes but only the one where the type
attribute equals 2. Again if you have more than one node matching this then
the SelectSingleNode will return to you the first one it finds.

Hope this helps
Mark R Dawson.

baggy said:
Hi,

I need help retrieving the value of node.
My XML looks like:-

origXML = "<gb><customerid type = '1'></customerid><customerid type =
'2'>{EC96CDE1-9C58-4D03-8683-6C03F6C5A404}</customerid><modifiedby>{659BEE75-D47E-4394-872B-EFB5A8D3E055}</modifiedby><modifiedon>2005-08-11T01:22:10Z</modifiedon></gb>";

I have tried this amongst other things and can't get it:-


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = origXML;
XmlNode xmlNode = xmlDoc.SelectSingleNode("/gb/customerid");
string strCustomerId = xmlNode.InnerText.ToString();

Hope somebody can help

thanks in advance,
baggy
 
Back
Top