Problem with XML Deserialization C#
Posted
by alex
on Stack Overflow
See other posts from Stack Overflow
or by alex
Published on 2010-05-17T22:18:59Z
Indexed on
2010/05/18
1:40 UTC
Read the original article
Hit count: 490
I am having trouble with XML deserialization.
In a nutshell - I have 2 classes:
- SMSMessage
- SMSSendingResponse
I call an API that takes a bunch of parameters (represented by SMSMessage class)
It returns an XML response. The response looks like this:
<?xml version="1.0" encoding="utf-8"?>
<data>
<status>1</status>
<message>OK</message>
<results>
<result>
<account>12345</account>
<to>012345678</to>
<from>054321</from>
<message>Testing</message>
<flash></flash>
<replace></replace>
<report></report>
<concat></concat>
<id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
</result>
</results>
</data>
Here is the SMSMessage class (with the xml serialization attributes so far)
using System.Xml.Serialization;
namespace XMLSerializationHelp
{
[XmlRoot("results")]
public class SMSMessage
{
public string To
{
get
{
return Result.To;
}
}
public string From
{
get
{
return Result.From;
}
}
public string Message
{
get
{
return Result.Message;
}
}
[XmlElement("result")]
public Result Result { get; set; }
}
}
Here is SMSMessageSendingResponse:
using System.Xml.Serialization;
namespace XMLSerializationHelp
{
[XmlRoot("data")]
public class SMSSendingResponse
{
//should come from the results/result/account element. in our example "12345"
public string AccountNumber
{
get
{
return SMSMessage.Result.AccountNumber;
}
}
//should come from the "status" xml element
[XmlElement("status")]
public string Status { get; set; }
//should come from the "message" xml element (in our example - "OK")
[XmlElement("message")]
public string Message { get; set; }
//should come from the "id" xml element (in our example - "f8d3eea1cbf6771a4bb02af3fb15253e")
public string ResponseID
{
get
{
return SMSMessage.Result.ResponseID;
}
}
//should be created from the results/result element - ignore flash, replace, report and concat elements for now.
[XmlElement("results")]
public SMSMessage SMSMessage { get; set; }
}
}
Here is the other class (Result) - I want to get rid of this, so only the 2 previously mentioned classes remain
using System.Xml.Serialization;
namespace XMLSerializationHelp
{
[XmlRoot("result")]
public class Result
{
[XmlElement("account")]
public string AccountNumber{ get; set; }
[XmlElement("to")]
public string To { get; set; }
[XmlElement("from")]
public string From { get; set; }
[XmlElement("message")]
public string Message { get; set; }
[XmlElement("id")]
public string ResponseID { get; set; }
}
}
I don't want SMSMessage to be aware of the SMSSendingResponse - as this will be handled by a different part of my application
© Stack Overflow or respective owner