Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Deserialize XML by reverse-engineering class def

Deserialize XML by reverse-engineering class def

Scheduled Pinned Locked Moved C#
jsonhelpcsharpcomxml
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ed 54
    wrote on last edited by
    #1

    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?

    G 1 Reply Last reply
    0
    • E Ed 54

      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?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Are you sure that the XML is the result of serializing an object? --- b { font-weight: normal; }

      E 1 Reply Last reply
      0
      • G Guffa

        Are you sure that the XML is the result of serializing an object? --- b { font-weight: normal; }

        E Offline
        E Offline
        Ed 54
        wrote on last edited by
        #3

        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?

        G 1 Reply Last reply
        0
        • E Ed 54

          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?

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          E 1 Reply Last reply
          0
          • G Guffa

            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; }

            E Offline
            E Offline
            Ed 54
            wrote on last edited by
            #5

            That's a good idea, thanks. I already built a class that I think matches the XML. Should be simple to serialize an object and compare it to the XML I'm trying to deserialize.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups