3

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?

detale
  • 12,482
  • 4
  • 41
  • 42

3 Answers3

5

If you want to deserialize that as XML, then use an XML type (either XElement or XmlElement should work) - see code below.

public class StackOverflow_11234676
{
    const string 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>";

    public class SetProfile
    {
        public Guid sessionId;
        public XElement profileDataXml;
    }
    public static void Test()
    {
        string inputXML = XML;
        XmlSerializer xs = new XmlSerializer(typeof(SetProfile));
        using (TextReader reader = new StringReader(inputXML))
        {
            SetProfile obj = (SetProfile)xs.Deserialize(reader);
            Console.WriteLine(obj.profileDataXml);
        }
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
1

I would say that you can deserialize this XML.

Take a look at this article: Attributes That Control XML Serialization

Easiest way to get it working is to use REVERSE approach. Create classes and apply xml serialization attributes and experiment with serialization until you get the same xml result as posted. Once you get the same xml, your deserialization will work.

Dusan
  • 5,000
  • 6
  • 41
  • 58
  • `XmlAnyElementAttribute` works for me when wanting to preserve XML elements that my code base doesn't have a schema for. `[XmlAnyElement] public XmlElement[] wildcardElements` – GhostRavenstorm Mar 22 '22 at 16:03
0

I would use Xml.Linq for this.

setProfile obj = new setProfile();
var doc = XDocument.Parse(yourXml);
obj.sessionID = doc.Root.Element("sessionID").value;
obj.profileDataXml = doc.Root.Element("profileDataXml").value;
Whyrusleeping
  • 889
  • 2
  • 9
  • 20