1

I have the following XML structure:

<BaseResponse>
  <StatusCode>0</StatusCode>
  <Text>success</Text>
  <DataObject>1111-1111-2222-121212-12121</DataObject>
</BaseResponse>

I am using XmlSerializer in C# to serialize the XML into a C# object:

public class BaseResponse
{
    public int StatusCode { get; set; }
    public string Text { get; set; }
    public string DataObject { get; set; }

    public static BaseResponse Get(string xmlResponse)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(BaseResponse));
        using (StringReader reader = new StringReader(xmlResponse))
        {
            var responseObj = (BaseResponse)(serializer.Deserialize(reader));
            return responseObj;
        }
    }
}

This works without any problems. But in certain cases the node DataObject includes children as well. For Example like this (can be any children really):

<BaseResponse>
  <Status>0</Status>
  <Text>success</Text>
  <DataObject>
    <DetailStatus>
        <Author>Peter</Author>
        <DateCreated>21.08.2021 19:00:05</DateCreated>
    </DetailStatus>
  </DataObject>
</BaseResponse>

Now, with my existing serializing mechanism this XML cant be parsed. Is there any way to tell the XmlSerializer to parse the children of the node DataObject as string if there are any. So that BaseResponse.DataObject would return the children nodes as string.

Like so:

"<DetailStatus><Author>Peter</Author><DateCreated>21.08.2021 19:00:05</DateCreated></DetailStatus>"

Thanks in advance!

G43beli
  • 3,835
  • 4
  • 21
  • 28
  • 1
    Does [XML - Deserialize property as Xml sub-tree](https://stackoverflow.com/q/33741048/3744182) work for you? – dbc Sep 28 '21 at 14:42
  • There is also [How to prevent XmlSerialzer from escaping "nested XML"?](https://stackoverflow.com/q/38107517/3744182). – dbc Sep 28 '21 at 14:48
  • 1
    @dbc defining the property as type XmlNode works for me (your first link). thanks very much. wanna add it as answer, so I can accept it? – G43beli Sep 28 '21 at 15:55

0 Answers0