Indenting XML output?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Veeneman

    Indenting XML output?

    I'm having a problem getting XML output properly formatted. specifically,
    line breaks and indentation are missing, even though I'm using an
    XmlTextWriter with Formatting = Formatting.Inde nted.

    Here is the code that's generating the problem:

    static void Main(string[] args)
    {
    // Create an XML document
    XmlDocument document = new XmlDocument();

    // Add an XML declaration section
    XmlNode newNode = document.Create Node(XmlNodeTyp e.XmlDeclaratio n,"","");
    document.Append Child(newNode);

    // Add a root element
    XmlElement newElement = document.Create Element("DemoXm lDocument");
    XmlText newElementText = document.Create TextNode("This is the text of
    the root element");
    newElement.Appe ndChild(newElem entText);
    document.Append Child(newElemen t);

    // Get the root element
    XmlElement rootElement = document.Docume ntElement;

    // Add a child element
    newElement = document.Create Element("Sample Element");
    newElementText = document.Create TextNode("The text of the sample
    element");
    newElement.Appe ndChild(newElem entText);
    rootElement.App endChild(newEle ment);

    // Save the XML document to file
    string fileName = @"c:\temp\DemoD ocument.xml";
    XmlTextWriter writer = new XmlTextWriter(f ileName, Encoding.Defaul t);
    writer.Formatti ng = Formatting.Inde nted;
    document.Save(w riter);
    writer.Close();
    }

    Here's what the output from the program looks like:

    <?xml version="1.0"?>
    <DemoXmlDocumen t>This is the text of the root element<SampleE lement>The text
    of the sample element</SampleElement></DemoXmlDocument >

    The content is fine, but the formatting is a mess. Any ideas why? Thanks for
    your help.

    --
    David Veeneman
    Foresight Systems


  • David Veeneman

    #2
    Re: Indenting XML output?

    I found my answer. The XML formatter was being thrown off by the fact that I
    had included a text attribute in my root element. So, I changed my root
    element to the following:

    // Add a root element
    XmlElement newElement = document.Create Element("DemoXm lDocument");
    document.Append Child(newElemen t);

    The document now formats correctly.

    --
    David Veeneman
    Foresight Systems


    Comment

    • csharp@elfware.za.net

      #3
      Re: Indenting XML output?

      This will work aswell... using the XmlTextWriter.F ormatting =
      Formatting.Inde nted.
      Maybe easier
      public static void WriteIndentedXm l(string xml)
      {
      XmlDocument doc = new XmlDocument();
      try
      {
      doc.LoadXml(xml );
      }
      catch (Exception ex)
      {
      Assert.Fail(ex. ToString());
      }

      System.IO.Strin gWriter stringWriter = new System.IO.Strin gWriter();
      XmlTextWriter writer = new XmlTextWriter(s tringWriter);
      writer.Formatti ng = Formatting.Inde nted;
      doc.WriteTo(wri ter);
      writer.Flush();

      Console.WriteLi ne(stringWriter .ToString());
      }

      Comment

      • David Veeneman

        #4
        Re: Indenting XML output?

        I did find an answer to this--the problen was that I was adding element text
        to the root element, which was confusing the XML formatter. Elements that
        are designed to contain other elements shouldn't get element text.

        --
        David Veeneman
        Foresight Systems


        Comment

        Working...