Converting REST response using XmlSerializer?

missamo80missamo80 Registered Users Posts: 43 Big grins
Does anyone have sample code for taking a REST response and converting it to .NET objects using XmlSerializer? I thought it would be straightforward, but since the response starts with an rsp <?rsp>tag instead of an xml <?xml>tag, the default .NET serializer barfs saying it's not valid XML.

Neil

Comments

  • devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited January 28, 2007
    Neil,

    I am pretty sure that the root element doesn't need to be an xml tag.

    The response contains the correct xml header...
    &lt;?xml version="1.0" encoding="utf-8"?&gt;
    
    I don't currently have a version of .net available to confirm this. Are able to try writing a response to a file manually, then attempting to load the response that way using the Xml Serializer just to rule out that there is nothing else going on.

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • missamo80missamo80 Registered Users Posts: 43 Big grins
    edited January 28, 2007
    I figured it out.

    My .NET object is called "LoginResponse". But the response that comes back via REST always uses "rsp" as the starting tag. So .NET barfs when it tries to deserialize, because the object names don't match. If I change my object name from "LoginResponse" to "rsp" then everything works fine.

    This is, of course, useless, since I can't have every object in my app called "rsp". Sigh.

    Neil
  • GarethLewinGarethLewin Registered Users Posts: 95 Big grins
    edited June 17, 2007
    missamo80 wrote:
    I figured it out.

    My .NET object is called "LoginResponse". But the response that comes back via REST always uses "rsp" as the starting tag. So .NET barfs when it tries to deserialize, because the object names don't match. If I change my object name from "LoginResponse" to "rsp" then everything works fine.

    This is, of course, useless, since I can't have every object in my app called "rsp". Sigh.

    Neil
    I found this old question while searching for something else, and wanted to respond. Hopefully the original poster no longer is having an issue with this, but future people might have the same problem and find this post.

    I handle the issue by just manually drilling thru the rsp node of the XML and serialising from there.

    I hope to release the source to my smugmug .net dll that I am writing when it is closer to working, but here is a bit of code i hacked together to show the general way I handle this.
            /// responseStream is the response from a HttpWebRequest
            void Sample(Stream responseStream)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(responseStream);
    
                XmlElement rsp = (XmlElement)doc.SelectSingleNode("rsp");
                Debug.Assert(rsp.Name == "rsp");
    
                // Make sure we got the right method
                Debug.Assert(rsp.SelectSingleNode("method").InnerText == "smugmug.login.withPassword");
    
                XmlAttribute stat = (XmlAttribute)doc.SelectSingleNode("rsp/@stat");
                Debug.Assert(stat.Name == "stat");
                Debug.Assert(stat.Value == "ok"); // TODO: Need to handle failure results
    
                XmlSerializer serializer;
                XmlNode rootNode;
    
                serializer = new XmlSerializer(typeof(LoginWithPassword));
                rootNode = rsp.SelectSingleNode("Login");
    
                XmlReader xmlReader = new XmlNodeReader(rootNode);
                LoginWithPassword response = (LoginWithPassword)serializer.Deserialize(xmlReader);
            }
    
    
    Hope this helps someone.
Sign In or Register to comment.