cnxmax
asked on
Desearialize an XML string
I have a windows service built with VS 2012 vb.net 4.0
I want to desterilize this xml string:
<?xml version='1.0' encoding='UTF-8'?>
<response>
<result numFound="96">
<doc spendingCategory="Assistan ce">
<record_count>2</record_co unt>
<UniqueTransactionID>747cd 6362dcfa5a 2c1b2e4982 3b3fb5c</U niqueTrans actionID> </doc>
<doc spendingCategory="Assistan ce">
<record_count>3</record_co unt>
<UniqueTransactionID>6dfae 83f273289b 0185ba25a8 21f7e27</U niqueTrans actionID> </doc>
<doc spendingCategory="Assistan ce">
<record_count>4</record_co unt>
<UniqueTransactionID>3c713 f5add6cc3b 40042609a7 ae5262c</U niqueTrans actionID> </doc>
</result>
</response>
Into this structure:
Public Structure response
Dim result As result
End Structure
Public Structure result
Dim numFound As String
Dim doc() As doc
End Structure
Public Structure doc
Dim spendingCategory As String
Dim record_count As String
Dim UniqueTransactionID As String
End Structure
using this code:
Public Shared Sub DeSerialize_Response(ByVal sXML As String, ByRef oResponse As response)
If String.IsNullOrEmpty(sXML) Then
oResponse = Nothing
Exit Sub
End If
Dim serializer As New Xml.Serialization.XmlSeria lizer(GetT ype(Schedu le))
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade r, settings)
oResponse = DirectCast(serializer.Dese rialize(xm lReader__1 ), response)
End Using
End Using
End Sub
I am getting this error:
{"There is an error in XML document (2, 2)."} and this in innerEXCEPTION:
<response xmlns=''> was not expected.
Can you tell me what i'm doing wrong with either my structure or my code.
thanks.
I want to desterilize this xml string:
<?xml version='1.0' encoding='UTF-8'?>
<response>
<result numFound="96">
<doc spendingCategory="Assistan
<record_count>2</record_co
<UniqueTransactionID>747cd
<doc spendingCategory="Assistan
<record_count>3</record_co
<UniqueTransactionID>6dfae
<doc spendingCategory="Assistan
<record_count>4</record_co
<UniqueTransactionID>3c713
</result>
</response>
Into this structure:
Public Structure response
Dim result As result
End Structure
Public Structure result
Dim numFound As String
Dim doc() As doc
End Structure
Public Structure doc
Dim spendingCategory As String
Dim record_count As String
Dim UniqueTransactionID As String
End Structure
using this code:
Public Shared Sub DeSerialize_Response(ByVal
If String.IsNullOrEmpty(sXML)
oResponse = Nothing
Exit Sub
End If
Dim serializer As New Xml.Serialization.XmlSeria
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade
oResponse = DirectCast(serializer.Dese
End Using
End Using
End Sub
I am getting this error:
{"There is an error in XML document (2, 2)."} and this in innerEXCEPTION:
<response xmlns=''> was not expected.
Can you tell me what i'm doing wrong with either my structure or my code.
thanks.
ASKER
TommySzalapski, thanks for responding. I have made progress using your suggestions. they error messages are now gone. however, after deserialization my oResponse.result.doc object has a count of 0 and my numFound is "nothing".
This is what I am now using for the structure:
<XmlRoot("response")>
Public Structure response
Dim result As result
End Structure
Public Structure result
Dim numFound As String
<XmlArray>
<XmlArrayItem("doc")>
Dim doc As List(Of doc)
End Structure
The xml string is the same.
This is the code I am now using for deserialization :
Public Shared Sub DeSerialize_Response(ByVal sXML As String, ByRef oResponse As response)
If String.IsNullOrEmpty(sXML) Then
oResponse = Nothing
Exit Sub
End If
Dim serializer As New Xml.Serialization.XmlSeria lizer(GetT ype(respon se), New XmlRootAttribute("response "))
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade r, settings)
oResponse = DirectCast(serializer.Dese rialize(xm lReader__1 ), response)
End Using
End Using
End Sub
can you see why my oResponse.result.doc object has a count of 0 and my numFound is "nothing"
This is what I am now using for the structure:
<XmlRoot("response")>
Public Structure response
Dim result As result
End Structure
Public Structure result
Dim numFound As String
<XmlArray>
<XmlArrayItem("doc")>
Dim doc As List(Of doc)
End Structure
The xml string is the same.
This is the code I am now using for deserialization :
Public Shared Sub DeSerialize_Response(ByVal
If String.IsNullOrEmpty(sXML)
oResponse = Nothing
Exit Sub
End If
Dim serializer As New Xml.Serialization.XmlSeria
Dim settings As New XmlReaderSettings()
Using textReader As New StringReader(sXML)
Using xmlReader__1 As XmlReader = XmlReader.Create(textReade
oResponse = DirectCast(serializer.Dese
End Using
End Using
End Sub
can you see why my oResponse.result.doc object has a count of 0 and my numFound is "nothing"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
did what you said, worked like a charm
Did you not create the file with the same VB library? It should have put a namespace in there.
You can give it a blank namespace when you serialize, something like this (trying to translate from C# in my head).
Open in new window
Or you could add some attributes to your structure like
Open in new window