Now first of all paste a proper XML, as I assume it is response xml returned from some web service, if yes then there
has to be some name space or root element having namespace which you have not mentioned over here. So first of all provide that.
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Hi Kavita, I think XML is well formatted but YES, namesapces are missing there. Hence, we need to include namesapces dynamically over there. After that we can process.
Correct Me, If I am Wrong!!!
Please "Mark As Answer;", if this Post helps you.
Visit My Blog
jbear123
Member
558 Points
1256 Posts
xml with namespace manager - cant find node value?
Feb 28, 2012 05:06 PM|LINK
Hi,
I have code to extract xml from a url (uri). The xml has various web: namespaces which I'm trying to get around.
I cant seem to extract the information from the xml file. I get an object not set error. My xml and code is:
<SearchResponse Version="2.2">
<Query>
<SearchTerms>hippo</SearchTerms>
</Query>
<web:Web>
<web:Total>48300000</web:Total>
var uri = new Uri("http://api xml file url.com"); System.Net.WebRequest request = System.Net.WebRequest.Create(uri); System.Net.WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string xmlcontents = sr.ReadToEnd(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(xmlcontents); XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable); xnm.AddNamespace("web", "http://www.w3.org/2005/Atom"); totalresults.Text = "Results: " + doc.SelectSingleNode("SearchResponse/web:Web/web:Total", xnm).InnerText;Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: xml with namespace manager - cant find node value?
Mar 01, 2012 01:45 AM|LINK
Hello jbear123:)
Your xml contents aren't right:
1)Without ending tags for web:xxx。
2)Without ending tags for Searchresponse。
And what I'm thinking is that your web isn't declared as a Namespace。Plz redefine it and have a try。
Sample:
【xml】
<soapenv:Envelope
xmlns:soapenv="http://soapenv/"
xmlns:xsd="http://XXX"
xmlns:xsi="http://XXX">
<soapenv:Body >
<Info xmlns="http://Info">
<V>123</V>
<O>456</O>
<M>789</M>
</Info>
</soapenv:Body>
</soapenv:Envelope>
【result】
XPathNavigator xPathNavigator = new XPathDocument(xmlStream).CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xPathNavigator.NameTable);
nsmgr.AddNamespace("mySpaceSoapenv", "http://soapenv/");
nsmgr.AddNamespace("mySpaceInfo", "http://Info");
XPathNodeIterator nodes = xPathNavigator.Select("/mySpaceSoapenv:Envelope/mySpaceSoapenv:Body/mySpaceInfo:mobileEmailBoxInfo/mySpaceInfo:Version", nsmgr);
kuber.manral
Contributor
3051 Points
714 Posts
Re: xml with namespace manager - cant find node value?
Mar 01, 2012 03:42 AM|LINK
Hi jbear123,
please review following thread for same reported issue :
http://forums.asp.net/t/1772611.aspx/1?xml+selecting+single+node+
Hope it helps...
Visit My Blog
kavita_khand...
Star
9767 Points
1931 Posts
Re: xml with namespace manager - cant find node value?
Mar 01, 2012 09:47 AM|LINK
<SearchResponse Version="2.2">
<Query>
<SearchTerms>hippo</SearchTerms>
</Query>
<web:Web>
<web:Total>48300000</web:Total>
</web:Web>
</SearchResponse>
First of all, the above XML is not proper XML for any xml parser.
Because the web: xml namespace alis is not defined any where.
Your xml has to be
<SearchResponse Version="2.2" xmlns:web="some xml ns" >
<Query>
<SearchTerms>hippo</SearchTerms>
</Query>
<web:Web>
<web:Total>48300000</web:Total>
</web:Web>
</SearchResponse>
or
<SearchResponse Version="2.2" xmlns:web="some xml ns" >
<Query>
<SearchTerms>hippo</SearchTerms>
</Query>
<Web>
<web:Total>48300000</web:Total>
</Web>
</SearchResponse>
Only then you will be able to iterate to it.
Now first of all paste a proper XML, as I assume it is response xml returned from some web service, if yes then there
has to be some name space or root element having namespace which you have not mentioned over here. So first of all provide that.
I would love to change the world, but they wont give me the source code.
kuber.manral
Contributor
3051 Points
714 Posts
Re: xml with namespace manager - cant find node value?
Mar 01, 2012 10:04 AM|LINK
Hi, As I believe Thread poster wants to share his solution wich was initially raised by him here :
http://forums.asp.net/t/1772611.aspx/2/10?xml+selecting+single+node
Dut to some techincal issues, he could not post that there. So started a new thread to share his solution here at forum. ;)
Hi Jbear, please marked your thread As resolved As you already posted solution.
For a reference again, i am providing relevant code again here:
System.Net.WebRequest request = System.Net.WebRequest.Create(uri); System.Net.WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string xmlcontents = sr.ReadToEnd(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(xmlcontents); XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable); xnm.AddNamespace("web", "http://www.w3.org/2005/Atom"); totalresults.Text = "Results: " + doc.SelectSingleNode("SearchResponse/web:Web/web:Total", xnm).InnerText;Hi Kavita, I think XML is well formatted but YES, namesapces are missing there. Hence, we need to include namesapces dynamically over there. After that we can process.
Correct Me, If I am Wrong!!!
Visit My Blog