不希望反序列化XML <response xmlns =“”>

时间:2018-03-19 18:43:25

标签: xml vb.net deserialization

我试图从网络反序列化XML文件(即我对初始格式有0控制权)。我通过使用以下链接将XML转换为JSON,从VB.NET中的XML文件中创建了我的类:

XML - json

然后我使用了“特殊粘贴”#34; - &GT; &#34;将json粘贴为类&#34;在Visual Studio 2017中形成编辑菜单。到目前为止,这一切似乎都很好。我的课程如下:

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

我使用此代码反序列化我下载的XML文件:

Dim serializer As New XmlSerializer(GetType(Rootobject))

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

在XML和序列化方面,我完全迷失了,这只是我正在研究的宠物项目。错误消息出现在上面代码的第3行,并且是:

  

System.InvalidOperationException:&#39; XML文档(1,2)中存在错误。&#39;
  InvalidOperationException:不期望<response xmlns=''>

这是XML文档的开头:

<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>

1 个答案:

答案 0 :(得分:2)

XML元素和类的大小写不匹配。您有两种选择:

  • 重命名您的课程以匹配实际案例。
  • 向映射到实际名称的类添加属性。

我会推荐第二种方式。对于Response类,它看起来像:

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

您不应该需要任何其他属性,因为这些类是在根类中明确说明的。