0

I have a xml string in a json data field. I want to extract that value and compare that to the database's value for that field.

I used xsd.exe to generate the class for that xml (saw from here). I am using that class to deserialize the xml response. Then i used the method from here to deserialize.

I used

`XmlSerializer serializer1 = new XmlSerializer(typeof(class_gen_from_xml))

In the below code, I extracted the xml source from the json response and then did as below:

string xmlSource = "<ResultSet><Result precision=\"address\">    <Latitude>47.643727</Latitude></Result></ResultSet>";

XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
ResultSet output;

using (StringReader reader = new StringReader(xmlSource))
{
   output = (ResultSet)serializer.Deserialize(reader);
}

` And I am getting an Exception and debugging reveals nothing at all. Is there something I am missing in the code ?

Community
  • 1
  • 1
Novak007
  • 426
  • 1
  • 9
  • 23

1 Answers1

1

Probably something wrong with your ResultSet class, this works fine for me:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ResultSet
{
    private ResultSetResult[] resultField;

    [System.Xml.Serialization.XmlElementAttribute("Result")]
    public ResultSetResult[] Result
    {
        get
        {
            return this.resultField;
        }
        set
        {
            this.resultField = value;
        }
   }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResultSetResult
{
    private decimal latitudeField;
    private string precisionField;

    public decimal Latitude
    {
        get
        {
            return this.latitudeField;
        }
        set
        {
           this.latitudeField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string precision
    {
        get
        {
            return this.precisionField;
        }
        set
        {
            this.precisionField = value;
        }
    }
}

With your de-serialization code:

static void Main(string[] args)
{
    string xmlSource = "<ResultSet><Result precision=\"address\">    <Latitude>47.643727</Latitude></Result></ResultSet>";

    XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
    ResultSet output;

    using (StringReader reader = new StringReader(xmlSource))
    {
        output = (ResultSet)serializer.Deserialize(reader);
    }
}
DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • I used it with another class although and I am getting the NullReferenceException. – Novak007 Jun 05 '15 at 09:19
  • No I tried to use it with another xml and generated its class and that is not working, could there be something that is missing or by the exception could you suggest a direction to look for to resolve the exception? – Novak007 Jun 05 '15 at 09:24
  • I am getting The system cannot find the file specified. which is surprsing. – Novak007 Jun 05 '15 at 09:40
  • 1
    @Novak007 You tried to use what? The same deserialization code or the class with another XML structure? Why would you expect that to work? It's based on the `ResultSet` XML... And your error message is completely unrelated to your question. You need to post the rest of your code. – DGibbs Jun 05 '15 at 09:44
  • @DrGibbs The same deserialization code with a different xml structure and for that I also generated its own class. – Novak007 Jun 05 '15 at 09:46
  • 1
    @Novak007 Does this work for the example in your OP? Did you update the types in the de-serialization code? Post the code/XML, we can't help if we can't see it. – DGibbs Jun 05 '15 at 09:51
  • Unable to generate temporary class, Error CS0030 cANNOT CONVERT TYPE – Novak007 Jun 05 '15 at 10:15
  • 1
    @Novak007 The error message is meaningless unless we can see your code. My answer successfully deserializes the `ResultSet` XML you've provided, it seems like you have an entirely different problem now? – DGibbs Jun 05 '15 at 10:16
  • could you once take a look here Thanks a lot for your help. – Novak007 Jun 05 '15 at 10:20