What's the easiest way in VB.NET to parse this XML?
Here's an example of the full source:
I believe that the XML can be read directly into a class structure that matches the XML's structure.
Let's take a bit of the XML from the above example,
<?xml version="1.0" encoding="UTF-8" ?>
- <kml xmlns="http://earth.google.com/kml/2.0">
- <Response>
<name>1321 herbert street, Warren, MI</name>
<Status>X</Status>
</Response>
</kml>
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?
For example, the kml part is confusing..
I started out with this class...
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)
..I get a parsing error.
I also tried this:
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...
thx
Follow up question.
How do I handle nested collections, for example, the Placemark collection in this example?