How to retain XML string as a string field during XML deserialization

Posted by detale on Stack Overflow See other posts from Stack Overflow or by detale
Published on 2012-06-27T20:54:37Z Indexed on 2012/06/27 21:16 UTC
Read the original article Hit count: 489

Filed under:
|
|

I got an XML input string and want to deserialize it to an object which partially retain the raw XML.

<SetProfile>
  <sessionId>A81D83BC-09A0-4E32-B440-0000033D7AAD</sessionId>
  <profileDataXml>
    <ArrayOfProfileItem>
      <ProfileItem>
        <Name>Pulse</Name>
        <Value>80</Value>
      </ProfileItem>
      <ProfileItem>
        <Name>BloodPresure</Name>
        <Value>120</Value>
      </ProfileItem>
    </ArrayOfProfileItem>
  </profileDataXml>
</SetProfile>

The class definition:

public class SetProfile
{
    public Guid sessionId;
    public string profileDataXml;
}

I hope the deserialization syntax looks like

string inputXML = "..."; // the above XML
XmlSerializer xs = new XmlSerializer(typeof(SetProfile));
using (TextReader reader = new StringReader(inputXML))
{
    SetProfile obj = (SetProfile)xs.Deserialize(reader);
    // use obj ....
}

but XMLSerializer will throw an exception and won't output < profileDataXml >'s descendants to "profileDataXml" field in raw XML string.

Is there any way to implement the deserialization like that?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml