Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use XmlSerializer to insert an xml string

I have defined the following class:

public class Root
{
    public string Name;
    public string XmlString;
}

and created an object:

Root t = new Root 
         {  Name = "Test", 
            XmlString = "<Foo>bar</Foo>" 
         };

When I use XmlSerializer class to serialize this object, it will return the xml:

<Root>
  <Name>Test</Name>
  <XmlString>&lt;Foo&gt;bar&lt;/Foo&gt;</XmlString>
</Root>

How do I make it not encode my XmlString content so that I can get the serialized xml as

<XmlString><Foo>bar</Foo></XmlString>

Thanks, Ian

like image 709
Hengyi Avatar asked Jun 29 '09 23:06

Hengyi


People also ask

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Why do we use XmlSerializer class?

Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. Call the Serialize method with the parameters of the StreamWriter and object to serialize.


2 Answers

You can limit the custom serialization to just the element that needs special attention like so.

public class Root
{
    public string Name;

    [XmlIgnore]
    public string XmlString
    {
        get
        {
            if (SerializedXmlString == null)
                return "";
            return SerializedXmlString.Value;
        }
        set
        {
            if (SerializedXmlString == null)
                SerializedXmlString = new RawString();
            SerializedXmlString.Value = value;
        }
    }

    [XmlElement("XmlString")]
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public RawString SerializedXmlString;
}

public class RawString : IXmlSerializable
{
    public string Value { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        this.Value = reader.ReadInnerXml();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteRaw(this.Value);
    }
}
like image 55
Cameron MacFarland Avatar answered Sep 20 '22 20:09

Cameron MacFarland


You can (ab)use the IXmlSerializable interface an XmlWriter.WriteRaw for that. But as garethm pointed out you then pretty much have to write your own serialization code.

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace ConsoleApplicationCSharp
{
  public class Root : IXmlSerializable
  {
    public string Name;
    public string XmlString;

    public Root() { }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
      writer.WriteElementString("Name", Name);
      writer.WriteStartElement("XmlString");
      writer.WriteRaw(XmlString);
      writer.WriteFullEndElement();
    }

    public void ReadXml(System.Xml.XmlReader reader) { /* ... */ }
    public XmlSchema GetSchema() { return (null); }
    public static void Main(string[] args)
    {
      Root t = new Root
      {
        Name = "Test",
        XmlString = "<Foo>bar</Foo>"
      };
      System.Xml.Serialization.XmlSerializer x = new XmlSerializer(typeof(Root));
      x.Serialize(Console.Out, t);
      return;
    }
  }
}

prints

<?xml version="1.0" encoding="ibm850"?>
<Root>
  <Name>Test</Name>
  <XmlString><Foo>bar</Foo></XmlString>
</Root>
like image 32
VolkerK Avatar answered Sep 23 '22 20:09

VolkerK