XML data parsing
-
Hi, I have a windows application that calls a web service. Web service returns a string response like I have to parse this string and get the values returned in etc. Number of ids returned by web service will depend on ABC number being passed. if user passed let's say 10 as ABC number then 10 ID's will be returned with string repeated 10 times with separatge Unique ID in each string. I want to retrive these values from the string that I have up in this message. How can I do that. Also, I am using vb.net. I would appreciate code sample in vb.net etc. Thanks Needy
-
Hi, I have a windows application that calls a web service. Web service returns a string response like I have to parse this string and get the values returned in etc. Number of ids returned by web service will depend on ABC number being passed. if user passed let's say 10 as ABC number then 10 ID's will be returned with string repeated 10 times with separatge Unique ID in each string. I want to retrive these values from the string that I have up in this message. How can I do that. Also, I am using vb.net. I would appreciate code sample in vb.net etc. Thanks Needy
Take the string that you get back and turn it into an XmlDocument:
Dim s as String = ...
Dim doc as XmlDocument = New XmlDocument()
doc.LoadXml(s)The difficulty is in the namespace. You have to take a few extra steps to do an XPath:
Dim xnm as XmlNamespaceManager
xnm = new XmlNamespaceManager(doc.NameTable)
xnm.AddNamespace("v1", "http://abc/2005/response")
Dim nodes as XmlNodeList
nodes = doc.SelectNodes("//v1:ABCs/v1:ABC", xnm)This gives you a collection of the "v1:ABC" nodes. Run that code and you'll see that
nodes.Count
is equal to the number of those "v1:ABC" nodes.
-
Take the string that you get back and turn it into an XmlDocument:
Dim s as String = ...
Dim doc as XmlDocument = New XmlDocument()
doc.LoadXml(s)The difficulty is in the namespace. You have to take a few extra steps to do an XPath:
Dim xnm as XmlNamespaceManager
xnm = new XmlNamespaceManager(doc.NameTable)
xnm.AddNamespace("v1", "http://abc/2005/response")
Dim nodes as XmlNodeList
nodes = doc.SelectNodes("//v1:ABCs/v1:ABC", xnm)This gives you a collection of the "v1:ABC" nodes. Run that code and you'll see that
nodes.Count
is equal to the number of those "v1:ABC" nodes.
it is giving me a null reference exception. when i get a count on nodes selected, it gives me number of nodes correctly. However, when i try to loop through the node list i am not able to get the values of the nodes Dim xNode As XmlNode Dim ValuesString As String For Each xNode In nodes ValuesString = xNode.Attributes.GetNamedItem("v1:ABC").Value Next I will want to receive these values and store them in database Please let me know what is wrong with my code. Line in bold is is where i am getting null reference exception. Thanks Needy
-
it is giving me a null reference exception. when i get a count on nodes selected, it gives me number of nodes correctly. However, when i try to loop through the node list i am not able to get the values of the nodes Dim xNode As XmlNode Dim ValuesString As String For Each xNode In nodes ValuesString = xNode.Attributes.GetNamedItem("v1:ABC").Value Next I will want to receive these values and store them in database Please let me know what is wrong with my code. Line in bold is is where i am getting null reference exception. Thanks Needy
Instead of "v1:ABC", you should be asking for the attribute "v1:ID". That's the name of the attribute.
-
Instead of "v1:ABC", you should be asking for the attribute "v1:ID". That's the name of the attribute.
I just found that out while debugging. thank you so much for your response. Out of curioucity, can I automatically fill up a datatable or dataset out of it? Because I will be calling a stored procedure to update the database with the ID values pulled from the xml string. thanks a lot for your response. I was stuck on this issue for a long time. Thanks Needy -- modified at 16:38 Thursday 22nd June, 2006
-
I just found that out while debugging. thank you so much for your response. Out of curioucity, can I automatically fill up a datatable or dataset out of it? Because I will be calling a stored procedure to update the database with the ID values pulled from the xml string. thanks a lot for your response. I was stuck on this issue for a long time. Thanks Needy -- modified at 16:38 Thursday 22nd June, 2006
Yes, you can create a
DataSet
off of the same string:Dim ds As DataSet = New DataSet()
ds.ReadXml(New StringReader(s))Where "s" is the string again.
StringReader
is in theSystem.IO
namespace. Just use the debugger to examine the contents of theDataSet
. You'll see tables in there matching the XML elements. -
Yes, you can create a
DataSet
off of the same string:Dim ds As DataSet = New DataSet()
ds.ReadXml(New StringReader(s))Where "s" is the string again.
StringReader
is in theSystem.IO
namespace. Just use the debugger to examine the contents of theDataSet
. You'll see tables in there matching the XML elements.v1:tcid="11111" xmlns:v1="http://abc/2005/response"> Hi, Dustin. I learned how to deal with node lists from your posts yesterday. I apprecidate your help. I need to get the value in which is basically v1:tcid="11111", Please shed the light again. Thanks Needy
-
v1:tcid="11111" xmlns:v1="http://abc/2005/response"> Hi, Dustin. I learned how to deal with node lists from your posts yesterday. I apprecidate your help. I need to get the value in which is basically v1:tcid="11111", Please shed the light again. Thanks Needy
Well,
v1:ABCMerchant
is your document element. You could either ask for the document element, or search for it with XPath.doc.DocumentElement.Attributes("v1:tcid")
-or-doc.SelectSingleNode("ABCMerchant", xnm).Attributes("v1:tcid")
You should do some reading up on XPath: http://www.w3schools.com/xpath/default.asp[^]