XML SelectSingleNode isn't returning a value
I am at a loss on this. I have tried many different examples I have by googling but I'm still having issues. This is my XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
-<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://blah" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
-<soap:Body>
-<ProcessRequestResponse xmlns="http://something.com/WebServices/">
-<ProcessRequestResult>
-<fiAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://something.com" xsi:schemaLocation="http://some xsd file" xmlns:xenc="http://www.w3.org/2001/xmlenc#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ITI="http://somepage.com/">
+<fiHeader Version="2.2">
-<Response TypeOfResponse="LoginRs" ResponseID="ReqID" More="false">
+<Status>
<Token>a really long string</Token>
</Response>
</fiAPI>
</ProcessRequestResult>
</ProcessRequestResponse>
</soap:Body>
</soap:Envelope>
I am trying to get the value of the Token node - "a really long string"
I can get all of the response xml in a textbox so I do know I am getting a response. This is the code I am trying to use:
Code:
Try
Dim objRequest As HttpWebRequest = CType(WebRequest.Create("http://some server page.asmx"), HttpWebRequest)
Dim StrData As String
Dim objXMLDoc As New XmlDocument()
objXMLDoc.LoadXml(strSOA) 'xml data to send
StrData = objXMLDoc.OuterXml
objRequest.Method = "POST"
Dim encoding As New System.Text.ASCIIEncoding()
Dim byte1 As Byte() = Text.Encoding.UTF8.GetBytes(StrData)
objRequest.ContentLength = byte1.Length
objRequest.ContentType = "text/xml"
objRequest.KeepAlive = False
Dim streamToSend As Stream = objRequest.GetRequestStream()
streamToSend.Write(byte1, 0, byte1.Length)
streamToSend.Close()
Dim objResponse As HttpWebResponse = objRequest.GetResponse()
Dim StreamReader As StreamReader
StreamReader = New StreamReader(objResponse.GetResponseStream())
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(StreamReader.ReadToEnd())
txtResponse.Text = Server.HtmlDecode(xmlDoc.InnerXml)
'*******This is where it breaks. Nothing is being returned*************
Dim request As String = "Response"
Dim strToken As XmlNodeList
strToken = xmlDoc.DocumentElement.SelectNodes("ProcessRequestResponse")
Dim strTokenNode As Xml.XmlNode
For Each strTokenNode In strToken
txtToken.Text = strTokenNode.SelectSingleNode("Token").InnerText
Next
StreamReader.Close()
Catch ex As Exception
txtResponse.Text = ex.Message
End Try
End Sub
Any help will be greatly appreciated
Re: XML SelectSingleNode isn't returning a value
Yeah... this is the part where the documentation kind of fails I think ... the problem is the namespaces... you need to add a NamespaceManager object and add the soap namespace to it, and add that to the XmlDocument...it's not finding ProcessRequestResponse because the node is part of the soap namespace... well, one of the namespaces... there's three defined: xsd, xsi, and soap....
Code:
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://blah" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
So you need to add all three to and XmlNamespaceManager objet (I think I have the name right, be sure to look it up) and associate it with the XmlDocument (the example on line for the namespace manager will have a sample on how to do that) before you go selecting nodes. Once you have that setup, you shouldn't have any more problems, providing your xpath selections are correct.
-tg
Re: XML SelectSingleNode isn't returning a value
Greetings techgnome. I really appreciate the reply. this has been bugging me for awhile.
Namespaces huh? I will give that a try. Would I also add the namespaces (xenc, ds, and ITI) from the FiAPI node?
Re: XML SelectSingleNode isn't returning a value
Greetings techgnome. I really appreciate the reply. this has been bugging me for awhile.
Namespaces huh? I will give that a try. Would I also add the namespaces (xenc, ds, and ITI) from the FiAPI node?
Re: XML SelectSingleNode isn't returning a value
Yup... if you plan to select from them, anything that's got an xmlns needs/should to be added to the namespace manager.
-tg
Re: XML SelectSingleNode isn't returning a value
*bangs head on desk*
I need some more of your help. These are the two samples I found:
Code:
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope")
nsmgr.AddNamespace("xenc", "http://www.w3.org/2001/xmlenc#")
nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmlsig#")
nsmgr.AddNamespace("ITI", "http://www.ITIWnet.com")
'txtToken.Text = xmlDoc.SelectSingleNode("Token", nsmgr).InnerText
<--This gives the error "'Object reference not set to an instance of an object."
so I tried this:
Code:
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope")
nsmgr.AddNamespace("xenc", "http://www.w3.org/2001/xmlenc#")
nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmlsig#")
nsmgr.AddNamespace("ITI", "http://www.ITIWnet.com")
Dim strToken As XmlNodeList
strToken = xmlDoc.DocumentElement.SelectNodes("Response")
Dim strTokenNode As Xml.XmlNode
For Each strTokenNode In strToken
txtToken.Text = strTokenNode.SelectSingleNode("Token", nsmgr).InnerText
Next <--returns nothing
what in the world am I doing wrong?!
Re: XML SelectSingleNode isn't returning a value
you need a proper xPath...
this:
Code:
strToken = xmlDoc.DocumentElement.SelectNodes("Response")
Says: At the Document root, look for the element called "Response" ... but there isn't one...
It helps to remember XML documents are heirarchical... not flat...
Code:
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://blah" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ProcessRequestResponse xmlns="http://something.com/WebServices/">
<ProcessRequestResult>
<fiAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://something.com" xsi:schemaLocation="http://some xsd file" xmlns:xenc="http://www.w3.org/2001/xmlenc#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ITI="http://somepage.com/">
<Response TypeOfResponse="LoginRs" ResponseID="ReqID" More="false">
Each node is like a directory... to get to the next node down, you have to go through the one before... Envelope (or rather soap:Envelope is your document root... soap:Body is it's child node... below that is ProcessRequestResponse, below that is ProcessRequestResult, below that is fiAPI and below that is (finally) Response.... so the xPath is:
soap:Body\ProcessRequestResponse\ProcessRequestResult\fiAPI\Response
xmlDoc.DocumentElement.SelectNodes("soap:Body\ProcessRequestResponse\ProcessRequestResult\fiAPI\Resp onse")
Also.. always return your object and test for it... don't try to use the .InnerText on it directly w/o first testing it unless your 1000% sure it's there.
-tg
Re: XML SelectSingleNode isn't returning a value
This is starting to make more sense but now I get this error, 'soap:Body\ProcessRequestResponse\ProcessRequestResult\fiAPI\Response\Token' has an invalid token.
it bombs on this line:
Code:
txtToken.Text = xmlDoc.SelectSingleNode("soap:Body\ProcessRequestResponse\ProcessRequestResult\fiAPI\Response\Token", nsmgr).InnerText
but I don't see any invalid token. Where should I be looking?
Re: XML SelectSingleNode isn't returning a value
This is why I sometimes hate XML & XPathing... it's picky... try taking off the soap: from the font of it... if that fails, you may have to try getting it one level at a time, seeing where it fails and then adjusting as needed.
-tg
Re: XML SelectSingleNode isn't returning a value
Quote:
Originally Posted by
techgnome
This is why I sometimes hate XML & XPathing... it's picky... try taking off the soap: from the font of it... if that fails, you may have to try getting it one level at a time, seeing where it fails and then adjusting as needed.
-tg
I did why I was getting the "Invalid Token" error. it was the back slashes. I had to use the forward slashes but not I get this error:
error "Object reference not set to an instance of an object."
on this line:
txtToken.Text = xmlDoc.SelectSingleNode("//Body/ProcessRequestResponse/", nsmgr).InnerText
I did as you suggested by removing the soap: and trying one level at a time but its always the same error, not matter which level I'm at.
any ideas?
So...I went the easy route and just stripped out all of the namespaces and am able to get the values I needed. Probably not the best solution but it works, for now...
Re: XML SelectSingleNode isn't returning a value
Hello Sir
Here is the code and final output for your issue.
Read whole xml into one variable, and read it as per following code.
XmlDocument docxml = new XmlDocument();
docxml.LoadXml(xmlValue);
String strToken nodeValue = ((System.Xml.XmlElement)(new System.Linq.SystemCore_EnumerableDebugView(docxml.ChildNodes[1].ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes[0].ChildNodes).Items[0])).InnerXml;
Final output is this
"a really long string"
I have attached screenshot also. I go through some xml tutorials and finally did it for you.
I hope this will help you :)
Re: XML SelectSingleNode isn't returning a value
Hi, I forgot to add tutorials link which I got reference. you may refer this xml tutorials as it has lots of xml examples.