反序列化XML< response xmlns =''>没想到 [英] Deserializing XML <response xmlns=''> was not expected

查看:107
本文介绍了反序列化XML< response xmlns =''>没想到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后我使用了特殊粘贴->在Visual Studio 2017中,从编辑菜单中粘贴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

我正在使用此代码反序列化XML文件ownload:

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

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

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:'XML文档中存在错误( 1,2)。'

InvalidOperationException:< response xmlns =''> 没想到。

以下是XML文档的开头:

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>


推荐答案

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

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


  • 重命名您的类以匹配实际情况。

  • 将属性添加到

我建议使用第二种方法。对于Response类,它看起来像:

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.

这篇关于反序列化XML&lt; response xmlns =''&gt;没想到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆