XmlSerializer - Indent as tab instead of spaces

N

Norbert Pürringer

Hello,

does anyone know how to serialize an object to xml by using tabs as
indent instead of spaces.

My serializer code looks like following:

public static XmlDocument Serialize(object serializableObject)
{
XmlSerializer responseSerializer = new
XmlSerializer(serializableObject.GetType());

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
try
{
ns.Add("", null);
ns.Add("xsd", "http://www.w3.org/2001/XMLSchema-instance");
responseSerializer.Serialize(writer, serializableObject, ns);
}
catch (Exception ex)
{
if (ex is InvalidOperationException)
throw ex.InnerException;
else
throw ex;
}
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(sb.ToString());
return xmlResponse;
}

Thank you,
Norbert
 
M

Marc Gravell

You need to write to an XmlWriter (which wraps, for example, your
StringWriter, StreamWriter, or whatever) which has been created with
suitable XmlWriterSettings - as below.

Marc

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
[Serializable]
public class Foo
{
public string Name { get; set; }
public int ShoeSize { get; set; }
}
static class Program
{
static void Main() {
Foo foo = new Foo { Name = "Fred", ShoeSize = 12 };

XmlWriterSettings settings = new XmlWriterSettings();
settings.IndentChars = "\t";
settings.Indent = true;

using(StringWriter sw = new StringWriter())
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
new XmlSerializer(typeof(Foo)).Serialize(xw, foo);
string xml = sw.ToString();
Console.WriteLine(xml);
}

}
}
 
A

Adam Benson

If you can alter things to use an XMLTextWriter that has an IndentChar field
:

System.IO.StringWriter sw = new System.IO.StringWriter();

XmlTextWriter xmltw = new XmlTextWriter(sw);

xmltw.Formatting = Formatting.Indented;

xmltw.IndentChar = '\x09';


HTH,

Adam.
=======
 
J

Jon Skeet [C# MVP]

If you can alter things to use an XMLTextWriter that has an IndentChar field

System.IO.StringWriter sw = new System.IO.StringWriter();

XmlTextWriter xmltw = new XmlTextWriter(sw);
xmltw.Formatting = Formatting.Indented;
xmltw.IndentChar = '\x09';

While you can do it this way, I prefer Marc's way of using
XmlWriterSettings. I also like to combine it with object initializers
in C# 3, which lets you do it all in one go if you want:

using (XmlWriter xw = XmlWriter.Create(sw,
new XmlWriterSettings { IndentChars="\t", Indent=true }))
{
...
}

Jon
 
N

Norbert Pürringer

Thank you for your advise using the XmlWriterSettings.

But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?

Thank you,
Norbert
 
J

Jon Skeet [C# MVP]

Thank you for your advise using the XmlWriterSettings.

But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?

It can't - the document itself has no concept of indentation. It's how
it's written that decides it. You'll need to save using a writer with
the appropriate settings.

Jon
 
M

Martin Honnen

Norbert said:
But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?

Have you tried to set
xmlDoc.PreserveWhitespace = true;
before you load?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top