Align Attributes when Formatting XML using LINQ to XML

A few years ago, I wrote a blog post that showed how to align attributes when formatting XML using LINQ to XML. Here is an extension method that uses that technique.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
    public static class Extensions
        public static string ToStringAlignAttributes(this XContainer xContainer)
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.NewLineOnAttributes = true;
            StringBuilder sb = new StringBuilder();
            using (XmlWriter xmlWriter = XmlWriter.Create(sb, settings))
            return sb.ToString();

    class Program
        static void Main(string[] args)
            XDocument doc = new XDocument(
                new XElement("Root",
                    new XAttribute("att1", 1),
                    new XAttribute("att2", 2),
                    new XAttribute("att3", 3),
                    new XElement("Child",
                        new XAttribute("att1", 1),
                        new XAttribute("att2", 2),
                        new XAttribute("att3", 3))));

            XElement el = new XElement("Root",
                new XAttribute("att1", 1),
                new XAttribute("att2", 2),
                new XAttribute("att3", 3),
                new XElement("Child",
                    new XAttribute("att1", 1),
                    new XAttribute("att2", 2),
                    new XAttribute("att3", 3)));

Update: May 5, 2011 – I initially wrote a more fancy version of this, but as it turns out, I got it wrong – it didn’t properly indent some cases of some XML documents, so am reverting the code. When I get a chance, I’ll work out the issues with the code that implements more fancy alignment.



  1. Otaku said,

    May 6, 2011 @ 12:33 am

    I’ve been meaning to explore XmlWriterSettings as I have a weird requirement in Open XML transformation. Basically, I need to do this:

    instead of this:

    As you may surmise, I’m dealing with Silverlight 3. 🙁 Your post gave me a good reminder to start looking at this in more detail to see if it fits the bill.

    (hopefully those codes look right when I push ‘post comment’)

  2. Eric White said,

    May 6, 2011 @ 1:25 am

    The new-line between those element is ‘insignificant white space’ in XML parlance. Does Silverlight 3 need specific ‘insignificant white space’?

  3. Otaku said,

    May 6, 2011 @ 2:10 pm

    It does and it doesn’t. Sorry, let me show you:

    In a <TextBlock/> you have child elements called runs (or inlines). In order for those runs to not have any whitespace between them, they have to be on the same line. So:

    <Run Text=”r” />
    <Run Text=”u” />
    <Run Text=”n” />

    would produce “r u n” whereas:

    <Run Text=”r” /><Run Text=”u” />
    <Run Text=”n” />

    would produce “run”.

    See how weird that is?

  4. Otaku said,

    May 6, 2011 @ 7:09 pm

    Well, I’ve opened at bounty at if you want to try your hand there and get 250 rep 🙂

  5. Proger said,

    May 9, 2011 @ 11:47 am

    I want automate a “Publisher” file, but I can’t find resource on this with openXML.

    this is possible or not ?


  6. Eric White said,

    May 9, 2011 @ 1:14 pm

    Hi Proger,

    Open XML encompasses only DOCX, XLSX, and PPTX. The publisher file format is not Open XML. Microsoft has no plans to publish specs for the publisher file format.


RSS feed for comments on this post · TrackBack URI

Leave a Comment