Solved

Desearialize an XML string

Posted on 2013-06-04
4
903 Views
Last Modified: 2013-06-05
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="Assistance">
            <record_count>2</record_count>
            <UniqueTransactionID>747cd6362dcfa5a2c1b2e49823b3fb5c</UniqueTransactionID>        </doc>
        <doc spendingCategory="Assistance">
            <record_count>3</record_count>
            <UniqueTransactionID>6dfae83f273289b0185ba25a821f7e27</UniqueTransactionID>        </doc>
        <doc spendingCategory="Assistance">
            <record_count>4</record_count>
            <UniqueTransactionID>3c713f5add6cc3b40042609a7ae5262c</UniqueTransactionID>        </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.XmlSerializer(GetType(Schedule))
        Dim settings As New XmlReaderSettings()

        Using textReader As New StringReader(sXML)
            Using xmlReader__1 As XmlReader = XmlReader.Create(textReader, settings)
                oResponse = DirectCast(serializer.Deserialize(xmlReader__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.
0
Comment
Question by:cnxmax
  • 2
  • 2
4 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39220625
It has to do with the response element being the root. It is expecting you to give a namespace (the ns is for namespace). So it is complaining because the namespace is blank (xmlns = '') note that it is two ' characters not one " character.

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).
Dim ns As New XmlSerializerNamespaces ns
ns.Add("", "")

serializer.Serialize(writer, object, ns)

Open in new window


Or you could add some attributes to your structure like
<XmlRoot("response")> _
Public Structure response
'... rest of code

Open in new window

0
 

Author Comment

by:cnxmax
ID: 39222899
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.XmlSerializer(GetType(response), New XmlRootAttribute("response"))
        Dim settings As New XmlReaderSettings()

        Using textReader As New StringReader(sXML)
            Using xmlReader__1 As XmlReader = XmlReader.Create(textReader, settings)
                oResponse = DirectCast(serializer.Deserialize(xmlReader__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"
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39222926
If the original problem is gone, then your best bet would be to close this question (since it was answered) and open a new one for the new problem. That way other people will look at it besides just me and it will have a better chance at a good fast answer.

I don't see anything right away that would explain why it's not working, but I don't have Windows on this computer so I can't play with .net code. I can try to look at it later when I get home, but again, since the original issue was solved, it makes sense to ask this as a new question. If you use the misc programming zone again, I will see the question in my email and it will remind me to look at it.
0
 

Author Closing Comment

by:cnxmax
ID: 39223054
did what you said, worked like a charm
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now