Cleaner Xml Serialization

Suppose you have a simple class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[Serializable, XmlRoot("LogEntry")]
public class LogFeeder
{
    private Guid sourceId;
    private String _Data;
    private String _Log;

    [XmlAttribute("DetailSourceId")]
    public Guid SourceId {
        get { return sourceId; }
        set { sourceId = value; }
    }

    [XmlAttribute("Data")]
    public string Data {
        get { return _Data; }
        set { _Data = value; }
    }

    [XmlAttribute("Log")]
    public string Log {
        get { return _Log; }
        set { _Log = value; }
    }
    public LogIUriFeeder() {}

    public LogIUriFeeder(Guid sourceId, string data, string log) {
        this.sourceId = sourceId;
        _Data = data;
        _Log = log;
    }
}

If you serialize with XmlSerializer you got this output as default

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-16"?>
<LogEntry 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    DetailSourceId="63509b61-f7a0-44ea-955a-38cce19aa13a" 
    Data="data" 
    Log="log" />

This is not very satisfying because I need to store it in database as xml fragment or appending to an existing xml node; so I do not care about the namespace or the xml declaration and I want to remove the xml declaration. The trick is to use an XmlWriter to specify format of outputted XML, and use a Blank Namespace to avoid namespace declaration, here is the code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static String ToXml(Object obj) {
    XmlSerializer ser = new XmlSerializer(obj.GetType(), "");
    StringWriter sw = new StringWriter();
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    settings.Indent = true;
    settings.NewLineOnAttributes = true;
    XmlSerializerNamespaces blank = new XmlSerializerNamespaces();
    blank.Add("", "");
    using (XmlWriter writer = XmlWriter.Create(sw, settings)) {
        ser.Serialize(writer, obj, blank);
    }
    return sw.ToString();
}

With this code the result of the serialization is

1
2
3
4
<LogEntry
  DetailSourceId="63509b61-f7a0-44ea-955a-38cce19aa13a"
  Data="data"
  Log="log" />

cleaner and shorter. With XmlWriterSettings you can also decide formatting, as example I prefer to have a new line on attributes, since my serialization is attribute centric.

alk.

Tags: XmlSerialization