Deserialize XML by reverse-engineering class def
-
Can I deserialize XML into an object by reverse-engineering the class definition from a sample XML document, if I don't have the actual class definition from which the XML was serialized? I'm making function calls to the Google Maps API geocoder using an HTTP request. The function call returns an XML file, which I want to extract data from. Here are the first few lines of the XML file:
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Response> <name>1600 Amphitheatre Parkway, Mountain View,CA</name> ....
And here are the first few lines of the class definition that I reverse-engineered from the XML:
\[XmlRootAttribute(ElementName = "kml", IsNullable = false)\] public class kmlJSON { public kmlJSON() { } public class Response { public String name; ....
Here is my C# routine to deserialize the data into an object of kml class:
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); XmlSerializer xs = new XmlSerializer(typeof(kmlJSON)); kmlJSON jsonObject = new kmlJSON(); jsonObject = (kmlJSON)xs.Deserialize(resStream); //try-catch block omitted for brevity
When I run it, I get the following error at
jsonObject = (kmlJSON)xs.Deserialize(resStream);
:\[InvalidOperationException: <kml xmlns='http://earth.google.com/kml/2.0'> was not expected.\] Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderkmlJSON.Read3\_kml() +91 \[InvalidOperationException: There is an error in XML document (1, 40).\]
Any ideas why XmlDeserializer is getting stuck on the root declaration of the XML file? Am I missing something simple, or must I have the exact class definition that the XML was serialized from?
-
Can I deserialize XML into an object by reverse-engineering the class definition from a sample XML document, if I don't have the actual class definition from which the XML was serialized? I'm making function calls to the Google Maps API geocoder using an HTTP request. The function call returns an XML file, which I want to extract data from. Here are the first few lines of the XML file:
<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.0"> <Response> <name>1600 Amphitheatre Parkway, Mountain View,CA</name> ....
And here are the first few lines of the class definition that I reverse-engineered from the XML:
\[XmlRootAttribute(ElementName = "kml", IsNullable = false)\] public class kmlJSON { public kmlJSON() { } public class Response { public String name; ....
Here is my C# routine to deserialize the data into an object of kml class:
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); XmlSerializer xs = new XmlSerializer(typeof(kmlJSON)); kmlJSON jsonObject = new kmlJSON(); jsonObject = (kmlJSON)xs.Deserialize(resStream); //try-catch block omitted for brevity
When I run it, I get the following error at
jsonObject = (kmlJSON)xs.Deserialize(resStream);
:\[InvalidOperationException: <kml xmlns='http://earth.google.com/kml/2.0'> was not expected.\] Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderkmlJSON.Read3\_kml() +91 \[InvalidOperationException: There is an error in XML document (1, 40).\]
Any ideas why XmlDeserializer is getting stuck on the root declaration of the XML file? Am I missing something simple, or must I have the exact class definition that the XML was serialized from?
-
Are you sure that the XML is the result of serializing an object? --- b { font-weight: normal; }
No, I'm not sure, and it probably wasn't. In fact, most likely it was generated by another language, like php or javascript. I didn't realize that was an issue. I figured that since an XML file was just text in XML format, with no special notation to indicate that it was serialized, it wouldn't matter whether it was serialized or generated by another method. As long as the class definition matched the XML format, I couldn't see how the deserializer would know any better. But I'm new to all this. In any event, I wound up parsing the XML with XmlTextReader, and it wasn't too hard since I'm only extracting one node right now. But if I wanted to extract multiple nodes, or a whole data record, deserialization would be a better solution if it were possible. Is it?
-
No, I'm not sure, and it probably wasn't. In fact, most likely it was generated by another language, like php or javascript. I didn't realize that was an issue. I figured that since an XML file was just text in XML format, with no special notation to indicate that it was serialized, it wouldn't matter whether it was serialized or generated by another method. As long as the class definition matched the XML format, I couldn't see how the deserializer would know any better. But I'm new to all this. In any event, I wound up parsing the XML with XmlTextReader, and it wasn't too hard since I'm only extracting one node right now. But if I wanted to extract multiple nodes, or a whole data record, deserialization would be a better solution if it were possible. Is it?
It's probably not possible to deserialize the XML straight off, but might be done if you make some adjustments to it first. You can try to create a class that matches the data in the XML, and serialize it to see what the XML should look like to be deserializable. --- b { font-weight: normal; }
-
It's probably not possible to deserialize the XML straight off, but might be done if you make some adjustments to it first. You can try to create a class that matches the data in the XML, and serialize it to see what the XML should look like to be deserializable. --- b { font-weight: normal; }