XmlSerializer - Indent as tab instead of spaces

  • Thread starter Thread starter Norbert Pürringer
  • Start date Start date
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
 
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);
}

}
}
 
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.
=======
 
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
 
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
 
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
 
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?
 
Back
Top