问题描述
限时送ChatGPT账号..VB.NET 中解析此 XML 的最简单方法是什么?
What's the easiest way in VB.NET to parse this XML?
以下是完整源代码的示例:
Here's an example of the full source:
查看源 XML
我相信可以将 XML 直接读入与 XML 结构匹配的类结构中.
I believe that the XML can be read directly into a class structure that matches the XML's structure.
让我们从上面的例子中获取一些 XML,
Let's take a bit of the XML from the above example,
<?xml version="1.0" encoding="UTF-8" ?>
- <kml xmlns="http://earth.google/kml/2.0">
- <Response>
<name>1321 herbert street, Warren, MI</name>
<Status>X</Status>
</Response>
</kml>
我想我可以使用此代码(取自另一个 SO 帖子)将 XML 转换为相应的对象
I thought I could use this code, taken from another SO posting, to convert the XML to corresponding objects
Imports System.IO
Imports System.Text
Public Class Xml
Public Shared Function SerializeToXMLString(ByVal ObjectToSerialize As Object) As String
Dim mem As MemoryStream = New MemoryStream()
Dim ser As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType())
ser.Serialize(mem, ObjectToSerialize)
Dim ascii As ASCIIEncoding = New ASCIIEncoding()
Return ascii.GetString(mem.ToArray())
End Function
Public Shared Function DeSerializeFromXMLString(ByVal TypeToDeserialize As System.Type, ByVal xmlString As String) As Object
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(xmlString)
Dim mem As MemoryStream = New MemoryStream(bytes)
Dim ser As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(TypeToDeserialize)
Return ser.Deserialize(mem)
End Function
End Class
..但问题是,我到底该如何定义这些对象——即使是缩小的简单示例?
..but the problem is, how the heck do I define these objects-even for the scaled-back simple example?
例如,kml 部分令人困惑..
For example, the kml part is confusing..
我从这门课开始...
Public Class kml
Public Class Response
Public Name As String
Public Status As String
End Class
End Class
..但是我运行这个代码:
..but wen I run this code:
Dim kml As kml
kml = CType(Xml.DeSerializeFromXMLString(GetType(kml), XmlDoc.OuterXml), kml)
..我收到解析错误.
我也试过这个:
Public Class kml
Private _Response As New Response
Public ReadOnly Property Response() As Response
Get
Return _Response
End Get
End Property
End Class
Public Class Response
Public name As String
Public Status As String
End Class
我知道我可能已经离开这里了.有人请把我推向正确的方向...
I know I'm probably way off here. Someone please push me in teh right direction...
谢谢
跟进问题.
如何处理嵌套集合,例如本例中的 Placemark 集合?
How do I handle nested collections, for example, the Placemark collection in this example?
推荐答案
您的 kml
和 Response
类应如下所示:
Your kml
and Response
classes should look like this:
Public Class kml
Private _Response As Response
Public Property Response() As Response
Get
Return _Response
End Get
Set(ByVal value As Response)
_Response = value
End Set
End Property
End Class
Public Class Response
Private _name As String
Public Property name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _Status As String
Public Property Status() As String
Get
Return _Status
End Get
Set(ByVal value As String)
_Status = value
End Set
End Property
End Class
然后你可以像这样反序列化:
Then you can deserialize like this:
Dim serializer As New XmlSerializer(GetType(kml), "http://earth.google/kml/2.0")
Dim result As kml = TryCast(serializer.Deserialize(mem), kml)
这篇关于VB.NET 中将此 XML 解析为对象的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论