1

I'm trying to deserialize an XML file from the web (i.e. I have 0 control over the initial format). I've made my classes from the XML file in VB.NET by converting the XML to JSON using this link:

XML - json

I've then used the 'Paste special" -> "Paste json as classes" form the edit menu in Visual Studio 2017. This all seems to be roughly OK so far. My classes are as follows:

Public Class Rootobject
    Public Property response As Response
End Class

Public Class Response
    Public Property parsererror As Parsererror
    Public Property area_name As String
    Public Property bounding_box As Bounding_Box
    Public Property country As String
    Public Property county As String
    Public Property latitude As String
    Public Property listing As Listing
End Class

Public Class Parsererror
    Public Property style As String
    Public Property h3() As String
    Public Property div As Div
End Class

Public Class Div
    Public Property style As String
    Public Property text As String
End Class

Public Class Bounding_Box
    Public Property latitude_max As String
    Public Property latitude_min As String
    Public Property longitude_max As String
    Public Property longitude_min As String
End Class

Public Class Listing
    Public Property agent_address As String
    Public Property agent_logo As String
    Public Property agent_name As String
    Public Property agent_phone As String
    Public Property category As String
    Public Property country As String
    Public Property country_code As String
    Public Property county As String
    Public Property description As String
End Class

I'm using this code to deserialize the XML file I download:

Dim serializer As New XmlSerializer(GetType(Rootobject))

Using reader As New FileStream(filename, FileMode.Open)
    respo = CType(serializer.Deserialize(reader), Rootobject)
End Using

I am completely lost when it comes to XML and serialization, this is just a pet project I'm working on. The error message comes at the 3rd line in the above code and is:

System.InvalidOperationException: 'There is an error in XML document (1, 2).'
InvalidOperationException: <response xmlns=''> was not expected.

Here's the start of the XML document:

<response>
<area_name>WA9</area_name>
<bounding_box>
<latitude_max>53.5027143349844</latitude_max>
<latitude_min>53.3581436650156</latitude_min>
<longitude_max>-2.64140084726415</longitude_max>
<longitude_min>-2.88405115273585</longitude_min>
</bounding_box>
<country>England</country>
<county>Merseyside</county>
<latitude>53.430429</latitude>
<listing>
<agent_address>
Nationwide Estate Agent, Head Office: Suite 7, First Floor, Cranmore Place, Cranmore Drive, Shirley, Solihull
</agent_address>
<agent_logo>
https://st.zoocdn.com/zoopla_static_agent_logo_(314863).png
</agent_logo>
<agent_name>Purplebricks, Head Office</agent_name>
<agent_phone>0121 721 9601</agent_phone>
<category>Residential</category>
<country>England</country>
<country_code>gb</country_code>
<county>Cheshire</county>
<description>
GSerg
  • 76,472
  • 17
  • 159
  • 346
FraserOfSmeg
  • 1,128
  • 2
  • 23
  • 41

1 Answers1

3

The capitalization of the XML elements and of your classes do not match. You have two options:

  • Rename your classes to match the actual cases.
  • Add attributes to the classes that map to the actual names.

I would recommend the second way. For the Response class, this would look like:

<XmlRoot(ElementName:="response")>
Public Class Response
    '...
End Class

You should not need any other attributes because the classes are explicitly stated in the root class.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Btw, you can also think about making the property names proper [Pascal Case](https://en.wikipedia.org/wiki/Camel_case) (the .Net standard style) and adding appropriate attributes to them (`XmlElement`). – Nico Schertler Mar 19 '18 at 19:26