【问题标题】:Deserializing XML <response xmlns=''> was not expected不期望反序列化 XML <response xmlns=''>
【发布时间】:2018-08-28 10:38:59
【问题描述】:

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

XML - json

然后我在 Visual Studio 2017 的编辑菜单中使用了“特殊粘贴”->“将 json 粘贴为类”。到目前为止,这一切似乎都还可以。我的类如下:

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: 'XML 文档 (1, 2) 中存在错误。'
InvalidOperationException: &lt;response xmlns=''&gt; 不是预期的。

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

【问题讨论】:

  • 请出示 XML 文档。
  • @NicoSchertler 有点大,我应该将它发布到网站并链接它还是直接跳到这里?
  • 不,如果外部资源对问题至关重要,请不要链接到它们。也许显示文档的前几行,以便我们了解。
  • @NicoSchertler 完成,谢谢
  • 尝试将GetType(Rootobject) 替换为GetType(Response)。并更改演员类型。

标签: xml vb.net deserialization


【解决方案1】:

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

  • 重命名您的类以匹配实际情况。
  • 向映射到实际名称的类添加属性。

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

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

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

【讨论】:

  • 顺便说一句,您还可以考虑使属性名称正确Pascal Case(.Net 标准样式)并向它们添加适当的属性(XmlElement)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 2021-03-23
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多