Discussion:
XmlSerializer - Indent as tab instead of spaces
(too old to reply)
Norbert Pürringer
2008-06-18 09:42:13 UTC
Permalink
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
Marc Gravell
2008-06-18 09:52:43 UTC
Permalink
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);
}

}
}
Adam Benson
2008-06-18 10:08:46 UTC
Permalink
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.
=======
Jon Skeet [C# MVP]
2008-06-18 10:18:35 UTC
Permalink
On Jun 18, 11:08 am, "Adam Benson"
Post by 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';
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
Norbert Pürringer
2008-06-18 11:28:56 UTC
Permalink
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
Jon Skeet [C# MVP]
2008-06-18 11:35:07 UTC
Permalink
Post by Norbert Pürringer
Thank you for your advise using the XmlWriterSettings.
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
Martin Honnen
2008-06-18 13:05:39 UTC
Permalink
Post by Norbert Pürringer
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?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Loading...