Formatting of XML created by DataContractSerializer
Asked Answered
H

5

59

Is there an easy way to get DataContractSerializer to spit out formatted XML rather then one long string? I don't want to change the tags or content in any way, just have it add line breaks and indentation to make the XML more readable?

<tagA>
   <tagB>This is</tagB>   
   <tagC>Much</tagC>
   <tagD>
      <tagE>easier to read</tagE>
   </tagD>
</tagA>


<tagA><tagB>This is</tagB><tagC>Much</tagC><tagD><tagE>harder to read</tagE></tagD></tagA>
Hypethral answered 10/4, 2009 at 22:56 Comment(1)
It's a bit delayed, but I added an update to my post.Anemoscope
R
98

As bendewey says, XmlWriterSettings is what you need - e.g. something like

var ds = new DataContractSerializer(typeof(Foo));

var settings = new XmlWriterSettings { Indent = true };

using (var w = XmlWriter.Create("fooOutput.xml", settings))
    ds.WriteObject(w, someFoos);
Ragland answered 10/4, 2009 at 23:40 Comment(0)
A
21

Take a look at the Indent property of the XmlWriterSettings

Update: Here is a good link from MSDN on How to: Specify the Output format on the XmlWriter

Additionally, here is a sample:

class Program
{
    static void Main(string[] args)
    {
        var Mark = new Person()
        {
            Name = "Mark",
            Email = "[email protected]"
        };

        var serializer = new DataContractSerializer(typeof(Person));

        var settings = new XmlWriterSettings()
        {
            Indent = true,
            IndentChars = "\t"
        };

        using (var writer = XmlWriter.Create(Console.Out, settings))
        {
            serializer.WriteObject(writer, Mark);
        }
        Console.ReadLine();
    }
}
public class Person
{
    public string Name { get; set; }
    public string Email { get; set; }
}
Anemoscope answered 10/4, 2009 at 23:5 Comment(0)
P
7

Be careful about adjusting whitespace in XML documents! Adjusting whitespace will make the XML more readable for us humans, but it may interfere with machine parsing.

According to the XML standard, whitespace is significant by default. In other words, as far as XML is concerned, white space is content.

If you feed your nicely formatted XML into an XML Document object, you will get a different result than the version that has no spaces or line breaks in it. You will get additional text nodes added to the version that has been formatted.

This MSDN article on XML White Space has several examples that show how tricky white space can be.

If you're formatting the XML only for human consumption, it doesn't matter. But if you try to round-trip your formatted document, you could run into trouble.

Since one of the key primary benefits of using DataContractSerializer is the ability to serialize objects and deserialize XML seamlessly, it's usually best to leave the ugly output alone.

I usually paste the output into NotePad++ and run an XML-tidy macro over it when I want to read it for debugging purposes.

Prefatory answered 19/2, 2010 at 20:0 Comment(2)
Shortcut for NotePad++ an XML-tidy macro see #8171240Cranston
White space occurring "outside" elements is not significant whitespace; it gets tricky depending on the content model (e.g. element vs mixed content), and for sure whitespace within an element or attribute is significant, so no indenting of multi-line element content! I find that DataContractSerializer has no problems deserializing "indented" documents of its own creation.Hanshansard
J
2
    public static string SerializeEntity<T>(T source)
    {
        using (MemoryStream ms = new MemoryStream())
        {

                NetDataContractSerializer serializer = new NetDataContractSerializer();
                serializer.Serialize(ms, source);
                return System.Text.Encoding.ASCII.GetString(ms.ToArray());

        }
    }

    public static T DeSerializeEntity<T>(string xml)
    {
        using (MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml)))
        {
                NetDataContractSerializer serializer = new NetDataContractSerializer();
                return (T)serializer.Deserialize(ms);
        }
    }
Jaclyn answered 16/11, 2009 at 8:47 Comment(1)
Shouldn't that be UTF8, not ASCII?Allowable
R
0

based on the other samples posted here that use XmlWriter, here's a version (from http://ClipFlair.codeplex.com) that works with streams (and Ionic.Zip library in specific) and also shows how the code is when you don't apply formatting (using conditional compilation - just comment out the #define to make it write unformatted XML)

#define WRITE_FORMATTED_XML

using System.Xml;

namespace ClipFlair.Windows
{

  public partial class BaseWindow : FloatingWindow
  {

    //...

    #if WRITE_FORMATTED_XML
    private static XmlWriterSettings XML_WRITER_SETTINGS = new XmlWriterSettings() { Indent=true, IndentChars="  "};
    #endif

    //...

    public virtual void SaveOptions(ZipFile zip, string zipFolder = "") //THIS IS THE CORE SAVING LOGIC
    {
      if (SavingOptions != null) SavingOptions(this, null); //notify any listeners

      View.Busy = true;
      try
      {
        ZipEntry optionsXML = zip.AddEntry(zipFolder + "/" + View.GetType().FullName + ".options.xml",
          new WriteDelegate((entryName, stream) =>
          {
            DataContractSerializer serializer = new DataContractSerializer(View.GetType()); //assuming current View isn't null
            #if WRITE_FORMATTED_XML
            using (XmlWriter writer = XmlWriter.Create(stream, XML_WRITER_SETTINGS))
              serializer.WriteObject(writer, View);
            #else
            serializer.WriteObject(stream, View);
            #endif
          }));
      }
      catch (Exception e)
      {
        MessageBox.Show("ClipFlair options save failed: " + e.Message); //TODO: find the parent window
      }
      finally
      {
        View.Busy = false; //in any case (error or not) clear the Busy flag
      }

      if (SavedOptions != null) SavedOptions(this, null); //notify any listeners
    }

    //...

  }

}
Ralston answered 22/2, 2013 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.