2

After I send a request with the required parameters in the response I get the following XML:

<content>
    <main>
        <IMGURL>image url</IMGURL>
        <IMGTEXT>Click Here</IMGTEXT>
        <TITLE>image title</TITLE>
        <IMGLINK>image link</IMGLINK>
    </main>
</content>

and I also made the following two classes:

[Serializable]
public class content
{
    private Main _main;
    public content()
    {
        _main = new Main();
    }
    public Main Main
    {
        get { return _main; }
        set { _main = value; }
    }
}

[Serializable]
public class Main
{
    public string IMGURL { get; set; }
    public string IMGTEXT { get; set; }
    public string TITLE { get; set; }
    public string IMGLINK { get; set; }
}

While debugging I can see that in the response I get the wanted results. However I'm having troubles deserializing the XML and populating the object.


Call to the method:

public static class ImageDetails
    {
        private static string _url = ConfigurationManager.AppSettings["GetImageUrl"];

        public static content GetImageDetails(string ua)
        {
            var contenta = new content();
            _url += "&ua=" + ua;
            try
            {
                WebRequest req = WebRequest.Create(_url);
                var resp = req.GetResponse();
                var stream = resp.GetResponseStream();

                //var streamreader = new StreamReader(stream);

                //var content = streamreader.ReadToEnd();



                var xs = new XmlSerializer(typeof(content));
                if (stream != null)
                {
                    contenta = (content)xs.Deserialize(stream);
                    return contenta;
                }
            }
            catch (Exception ex)
            {
            }
            return new content();
        }
    }
Johancho
  • 105
  • 2
  • 6
  • Please show us what you've tried. Also, the XmlSerializer ignores `[Serializable]`. – John Saunders Aug 28 '11 at 18:47
  • I've updated my question. Also, I'm honoured that you took the time to answer my question. I'm a fan of yours! – Johancho Aug 28 '11 at 18:53
  • Fans? Scary concept. Suggestion: get rid of that try/catch block. Also, stop changing `_url` in place. If you call that twice, you'll have multiple `ua` query params. – John Saunders Aug 28 '11 at 18:57
  • Tried what you suggested but it did not work. If I comment out the part starting from 'var xs = new XmlSerializer(typeof(content));' to 'return contenta' and uncomment '//var streamreader = new StreamReader(stream); //var content = streamreader.ReadToEnd(); ' I can see the XML. I must be doing something wrong in the part where i Deserialize the XML... While debugging I can see that the properties of Main are Null – Johancho Aug 28 '11 at 19:01
  • Suggestion: fill an instance of `content` with the data you'd like to see, then serialize it. That should show you what the XmlSerializer wants to see as input. – John Saunders Aug 28 '11 at 19:06
  • That's a great idea! I'll do that right away! – Johancho Aug 28 '11 at 19:09

1 Answers1

0

The serializer is case-sensitive. You either need to rename the property content.Main to main or add the attribute [XmlElement("main")] to it.

svick
  • 236,525
  • 50
  • 385
  • 514
  • For the record it is case sensitive and your code should read "Public Main main { .. }" or it will certainly not deserialize – Eddy Aug 28 '11 at 19:12
  • Your answer was correct. After following John's advice i saw that the object was serialized in the exact same format as the XML. Then i realised I hadn't changed the name of the property. Thank you guys!!! – Johancho Aug 28 '11 at 19:13