XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlContent);
XmlNode vValue = xmlDoc.SelectSingleNode("//value"); //Or whatever Xpath query
string strValue = vValue.InnerText; //Or get the value of textnode child
I think I have my xpath query wrong. If I am trying to get the value by the address element in the xml below, can you tell me if my query is right?
<?xml version="1.0" encoding="utf-8" ?>
- <HopStopResponse xmlns="http://www.hopstop.com/ws/">
<Disclaimer><a href="http://www.hopstop.com"><img src="http://www.hopstop.com/img/powered.gif" width="154" height="17" border="0" alt="Powered by HopStop"></a><br> These directions are informational only. No representation is made or warranty given as to their content, route usability or expeditiousness. User assumes all risk of use. Hopstop and its suppliers assume no responsibility for any loss or delay resulting from such use.</Disclaimer>
- <MapInfo>
<Address>1300 South El Camino Real, San Mateo, CA</Address>
<City>sanfrancisco</City>
<County>san mateo, ca</County>
<CountyCode>6081</CountyCode>
<CreatedByModule>m</CreatedByModule>
<Height>300</Height>
<Id>321123456</Id>
<State>dfgasdfgasdfasfasdfasd=</State>
<Title>1300 S EL CAMINO REAL, San Mateo, CA</Title>
<URL>http://www.hopstop.com/ws/map?321123456</URL>
<Width>400</Width>
<X>-122.31737</X>
<Y>37.5552</Y>
<Zip>94402</Zip>
</MapInfo>
- <ResponseStatus>
<ResultCode>200</ResultCode>
<ResultString>Map info updated.</ResultString>
</ResponseStatus>
</HopStopResponse
Your XPath looks right. I think most likely the namespace definition on the HopStopResponse element that's giving you trouble. Try looking at this article:
http://support.microsoft.com/kb/318545 and adding the proper namespaces to your XmlDocument before you ru your query. (I personally always hated the way .NEt handles this part of xml.)
Menno
Marked as answer by solomonnet on Apr 21, 2010 12:58 PM
Linq is fully supported on C#, and Linq to Xml is not a bad way to get your data. To just use XPath though, declaring the namespaces works fine. In fact, yesterday I wrote a little class that does something similar to what you need (but just from Facebook).
This code works fine:
public int GetFanCount()
{
string requestUrl = GetRequestUrl();
if (!String.IsNullOrEmpty(requestUrl))
{
WebClient client = new WebClient();
string faceBookData = client.DownloadString(requestUrl);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(faceBookData);
XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
xmlnsManager.AddNamespace("fb", "http://api.facebook.com/1.0/");
xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode xmlNode = xmlDoc.SelectSingleNode("//fb:fan_count/text()", xmlnsManager);
if (xmlNode != null)
{
int value = int.Parse(xmlNode.Value);
return value;
}
}
return 0;
}
We've drifted pretty far from your original question though. If you have more problems getting data out of your xml string, I suggest you close this question, and open a new one.
solomonNet
Member
14 Points
58 Posts
parsing xml element into a string variable
Apr 15, 2010 12:58 PM|LINK
Can someone tell me how to parse an httpwebrequest which the response returns xml?
I want the value of an element to be captured into a string variable. Maybe with xpath? Please can someone help me?
I am up the point where I have the whole xml in a single string variable, like this:
using{
(StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
string xmlContent = sr.ReadToEnd().Trim();
now what?
Menno van de...
Contributor
2764 Points
391 Posts
Re: parsing xml element into a string variable
Apr 15, 2010 02:36 PM|LINK
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlContent); XmlNode vValue = xmlDoc.SelectSingleNode("//value"); //Or whatever Xpath query string strValue = vValue.InnerText; //Or get the value of textnode childMenno
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: parsing xml element into a string variable
Apr 15, 2010 02:38 PM|LINK
You can parse you xml using xpath and get the nodes value, name , atributes.
try this:
http://stackoverflow.com/questions/241238/how-to-get-xpath-from-an-xmlnode-instance-c
Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
solomonNet
Member
14 Points
58 Posts
Re: parsing xml element into a string variable
Apr 16, 2010 01:12 PM|LINK
I think I have my xpath query wrong. If I am trying to get the value by the address element in the xml below, can you tell me if my query is right?
Menno van de...
Contributor
2764 Points
391 Posts
Re: parsing xml element into a string variable
Apr 16, 2010 06:50 PM|LINK
Your XPath looks right. I think most likely the namespace definition on the HopStopResponse element that's giving you trouble. Try looking at this article: http://support.microsoft.com/kb/318545 and adding the proper namespaces to your XmlDocument before you ru your query. (I personally always hated the way .NEt handles this part of xml.)
Menno
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: parsing xml element into a string variable
Apr 19, 2010 02:05 AM|LINK
Hey, you can try LINQ:
XDocument doc = XDocument.Parse("Your string");
XElement ele = doc.Root.Element("MapInfo").Element("Address");
ele.Value;
solomonNet
Member
14 Points
58 Posts
Re: parsing xml element into a string variable
Apr 20, 2010 01:55 PM|LINK
menno, i wish you could ellaborate more. What you said sounds right.
Decker, I don't think I can use LINQ with C#. Thanks anyway though.
Menno van de...
Contributor
2764 Points
391 Posts
Re: parsing xml element into a string variable
Apr 21, 2010 09:00 AM|LINK
Solomon,
Linq is fully supported on C#, and Linq to Xml is not a bad way to get your data. To just use XPath though, declaring the namespaces works fine. In fact, yesterday I wrote a little class that does something similar to what you need (but just from Facebook). This code works fine:
public int GetFanCount() { string requestUrl = GetRequestUrl(); if (!String.IsNullOrEmpty(requestUrl)) { WebClient client = new WebClient(); string faceBookData = client.DownloadString(requestUrl); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(faceBookData); XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable); xmlnsManager.AddNamespace("fb", "http://api.facebook.com/1.0/"); xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlNode xmlNode = xmlDoc.SelectSingleNode("//fb:fan_count/text()", xmlnsManager); if (xmlNode != null) { int value = int.Parse(xmlNode.Value); return value; } } return 0; }We've drifted pretty far from your original question though. If you have more problems getting data out of your xml string, I suggest you close this question, and open a new one.
Menno
solomonNet
Member
14 Points
58 Posts
Re: parsing xml element into a string variable
Apr 21, 2010 12:57 PM|LINK
menno,
can I see a quick example of the xml you were expecting from facebook?
I ask because I don't know how to add the correct namespaces for MY xml. Maybe you could help me with that.
Menno van de...
Contributor
2764 Points
391 Posts
Re: parsing xml element into a string variable
Apr 21, 2010 01:46 PM|LINK
http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=36922302396
<?xml version="1.0" encoding="UTF-8"?> <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true"> <page> <fan_count>949833</fan_count> </page> </fql_query_response>Menno