【发布时间】:2018-08-28 10:38:59
【问题描述】:
我正在尝试反序列化来自网络的 XML 文件(即我对初始格式的控制为 0)。通过使用此链接将 XML 转换为 JSON,我从 VB.NET 中的 XML 文件创建了我的类:
然后我在 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:<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>
【问题讨论】:
-
请出示 XML 文档。
-
@NicoSchertler 有点大,我应该将它发布到网站并链接它还是直接跳到这里?
-
不,如果外部资源对问题至关重要,请不要链接到它们。也许显示文档的前几行,以便我们了解。
-
@NicoSchertler 完成,谢谢
-
尝试将
GetType(Rootobject)替换为GetType(Response)。并更改演员类型。
标签: xml vb.net deserialization