1

I receive an object from another application and need to serialize it into XML. The object contains several properties and one of them contains already serialized part of XML.

How can I keep it as it is during serialization to XML?

Currently I am using XmlSerializer, but the problem is that it escapes some characters (< and >) and serializes property as a value of XML node.

Example, object structure:

public class SomeObject
{
    public string A{get;set;}
    public string B {get;set;}

    //contains part of XML
    public string C {get;set;}
}

Instance:

var a = new SomeObject
{
    A = "A",
    B = "B",
    C = "<C1>test</C1><C2>test 2</C2><C3>test 2</C3>"
};

I need to keep C value as it is:

<SomeObject>
  <A>A</A>
  <B>B</B>
  <C><C1>test</C1><C2>test 2</C2><C3>test 2</C3></C>
</SomeObject>

Instead, the result looks differently (which is expected, but not what I need):

<SomeObject>
  <A>A</A>
  <B>B</B>
  <C>&lt;C1&gt;test&lt;/C1&gt;&lt;C2&gt;test 2&lt;/C2&gt;&lt;C3&gt;test 2&lt;/C3&gt;</C>
</SomeObject>

Code that I am using for serialization:

 public string Serialize(object obj)
        {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                var xmlserializer = new XmlSerializer(obj.GetType(), "");

                var stringWriter = new StringWriter();
                var settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = true };

                using (var writer = XmlWriter.Create(stringWriter, settings))
                {
                    xmlserializer.Serialize(writer, obj,ns);
                    return stringWriter.ToString();
                }
        }

Of course, I can create another object where C will be type of class and deserialize my C to that object, and then serialize that object. It will be serialized to the correct object, exactly what I need. But I want to avoid all those additional serialization/deserialization operations.

Is there a way to specify for the serializer that the property should not be additionally serialized?

Wizard91
  • 46
  • 4
  • 1
    Use [`XmlAnyElementAttribute`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlanyelementattribute?view=netframework-4.8). See: [Populating ANY elements on a web service in C#](https://stackoverflow.com/q/42114531/3744182) or [C# - XML - Treat inner xml from a certain element as string](https://stackoverflow.com/q/7534955/3744182). Agree it's a duplicate of those? – dbc Oct 19 '19 at 16:31
  • Other option would be to implement `IXmlSerializable` as shown in [How do I use XmlSerializer to insert an xml string](https://stackoverflow.com/q/1061027/3744182), but I don't really recommend it. – dbc Oct 19 '19 at 16:32
  • 1
    @dbc, yes, using XmlAnyElement attribute works for me. It still requires to create a specific object for serialization that I tried to avoid (I don't have access to the original object that I receive and tried to serialize). Anyway it's better than full deserialization and serialization. Thanks – Wizard91 Oct 19 '19 at 17:02
  • 1
    Well if you don't want to use `XmlAnyElement` you can use `RawString` from [How do I use XmlSerializer to insert an xml string](https://stackoverflow.com/a/1061207/3744182). – dbc Oct 19 '19 at 17:15
  • 1
    @dbc, I am fine with XmlAnyElement solution. Thanks – Wizard91 Oct 19 '19 at 17:43

0 Answers0