XmlSerializer - Indent as tab instead of spaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Norbert_P=FCrringer?=

    XmlSerializer - Indent as tab instead of spaces

    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(objec t serializableObj ect)
    {
    XmlSerializer responseSeriali zer = new
    XmlSerializer(s erializableObje ct.GetType());

    StringBuilder sb = new StringBuilder() ;
    StringWriter writer = new StringWriter(sb );
    XmlSerializerNa mespaces ns = new XmlSerializerNa mespaces();
    try
    {
    ns.Add("", null);
    ns.Add("xsd", "http://www.w3.org/2001/XMLSchema-instance");
    responseSeriali zer.Serialize(w riter, serializableObj ect, ns);
    }
    catch (Exception ex)
    {
    if (ex is InvalidOperatio nException)
    throw ex.InnerExcepti on;
    else
    throw ex;
    }
    XmlDocument xmlResponse = new XmlDocument();
    xmlResponse.Loa dXml(sb.ToStrin g());
    return xmlResponse;
    }

    Thank you,
    Norbert
  • Marc Gravell

    #2
    Re: XmlSerializer - Indent as tab instead of spaces

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

    Marc

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Seri alization;
    [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 };

    XmlWriterSettin gs settings = new XmlWriterSettin gs();
    settings.Indent Chars = "\t";
    settings.Indent = true;

    using(StringWri ter sw = new StringWriter())
    using (XmlWriter xw = XmlWriter.Creat e(sw, settings))
    {
    new XmlSerializer(t ypeof(Foo)).Ser ialize(xw, foo);
    string xml = sw.ToString();
    Console.WriteLi ne(xml);
    }

    }
    }

    Comment

    • Adam Benson

      #3
      Re: XmlSerializer - Indent as tab instead of spaces

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

      System.IO.Strin gWriter sw = new System.IO.Strin gWriter();

      XmlTextWriter xmltw = new XmlTextWriter(s w);

      xmltw.Formattin g = Formatting.Inde nted;

      xmltw.IndentCha r = '\x09';


      HTH,

      Adam.
      =======


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: XmlSerializer - Indent as tab instead of spaces

        On Jun 18, 11:08 am, "Adam Benson"
        <Adam.Ben...@NO SPAMMYSPAM.omni bus.co.ukwrote:
        If you can alter things to use an XMLTextWriter that has an IndentChar field
        >
        System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
        >
        XmlTextWriter xmltw = new XmlTextWriter(s w);
        xmltw.Formattin g = Formatting.Inde nted;
        xmltw.IndentCha r = '\x09';
        While you can do it this way, I prefer Marc's way of using
        XmlWriterSettin gs. 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.Creat e(sw,
        new XmlWriterSettin gs { IndentChars="\t ", Indent=true }))
        {
        ...
        }

        Jon

        Comment

        • =?ISO-8859-1?Q?Norbert_P=FCrringer?=

          #5
          Re: XmlSerializer - Indent as tab instead of spaces

          Thank you for your advise using the XmlWriterSettin gs.

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

          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.LoadXml( stringWriter.To String());
          xmlDoc.Save(fil e);

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

          Thank you,
          Norbert

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: XmlSerializer - Indent as tab instead of spaces

            On Jun 18, 12:28 pm, Norbert Pürringer <thalio...@graf fiti.netwrote:
            Thank you for your advise using the XmlWriterSettin gs.
            >
            But the set Indent get lost, if I say something like that:
            >
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml( stringWriter.To String());
            xmlDoc.Save(fil e);
            >
            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

            Comment

            • Martin Honnen

              #7
              Re: XmlSerializer - Indent as tab instead of spaces

              Norbert Pürringer wrote:
              But the set Indent get lost, if I say something like that:
              >
              XmlDocument xmlDoc = new XmlDocument();
              xmlDoc.LoadXml( stringWriter.To String());
              xmlDoc.Save(fil e);
              >
              Any idea how to say the XmlDocument to preserve the set indent?
              Have you tried to set
              xmlDoc.Preserve Whitespace = true;
              before you load?

              --

              Martin Honnen --- MVP XML

              Comment

              Working...