I haven't found a way using .NET's XmlWriter and associated XmlWriterSettings to format an XML string in indented form exactly the way that Visual Studio does it with its auto-format command (Ctrl-E Ctrl-D, or, depending on keyboard mapping, Ctrl-K Ctrl-D).
I would like to do this because I habitually auto-format all my files in VS, both code and .config files. I have an installer app that updates .config files, and I would like to see actual diffs instead of the entire document changed.
I haven't explored all the different formatting options for auto-format, but I like each XML attribute to be on a separate line, with the first on the same line as the opening tag and subsequent ones lined up with the first, like this:
<asset assetId="12345"
bucket="default"
owner="nobody">
<file path="\\localhost\share\assetA.mov"/>
<metadata metadataId="23456"
key="asset_type"
value="video"/>
</asset>
I've tried formatting with XmlWriterSettings properties 'NewLineHandling = NewLineHandling.None' and 'NewLineOnAttributes = true', but that puts the first attribute below the opening tag and all attributes have the same indentation regardless of the # of characters in the element name, like this:
<asset
assetId="12345"
bucket="default"
owner="nobody">
<file
path="\\localhost\share\assetA.mov" />
<metadata metadataId="23456"
key="asset_type"
value="video" />
</asset>
Notice that the standard XmlWriter also ends attribute-only elements with " />" (extra space before the slash), which I dislike but not sure if that's the XML standard. I would think that Visual Studio uses the same API options readily available to developers, but I've yet to find those magical settings. Anyway, here's my format method:
public static string FormatXml( string xmlString, bool indented )
{
using ( TextReader textReader = new StringReader( xmlString ) )
using ( XmlReader xmlReader = new XmlTextReader( textReader ) )
{
using ( TextWriter textWriter = new StringWriter() )
{
var settings = new XmlWriterSettings();
if ( indented )
{
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineOnAttributes = true;
settings.NewLineHandling = NewLineHandling.None;
}
using ( var xmlWriter = XmlWriter.Create( textWriter, settings ) )
{
while ( xmlReader.Read() )
xmlWriter.WriteNode( xmlReader, false );
}
return textWriter.ToString();
}
}
}
I had misunderstood the question. Actually I don't know if there is a way to align the attributes in the way you shown. You could try to implement it yourself, something like this:
public static string FormatXml(string xmlString, bool indented)
{
using (TextReader textReader = new StringReader(xmlString))
using (XmlReader xmlReader = new XmlTextReader(textReader))
{
using (TextWriter textWriter = new StringWriter())
{
string indent = "";
string attributeIndent = "";
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
attributeIndent = "";
string element = xmlReader.Name;
textWriter.Write("{0}<{1}", indent, element);
if (!xmlReader.HasAttributes)
textWriter.WriteLine(">");
else
{
int actual = 1;
while (xmlReader.MoveToNextAttribute())
{
string content = String.Format("{0} {1}={2}", attributeIndent, xmlReader.Name, xmlReader.Value);
if (actual != xmlReader.AttributeCount)
textWriter.WriteLine(content);
else
textWriter.Write(content);
if (string.IsNullOrEmpty(attributeIndent))
attributeIndent = indent + Enumerable.Repeat<string>(" ", element.Length + 1).Aggregate((a, b) => a + b);
actual++;
}
xmlReader.MoveToElement();
textWriter.WriteLine(">");
attributeIndent = "";
}
if (!xmlReader.IsEmptyElement)
{
indent += " ";
textWriter.WriteLine(">");
}
else
textWriter.WriteLine("/>");
}
else if (xmlReader.NodeType == XmlNodeType.EndElement)
{
indent = indent.Substring(0, indent.Length - 2);
textWriter.WriteLine("{0}</{1}>", indent, xmlReader.Name);
}
else if (xmlReader.NodeType == XmlNodeType.Text)
{
textWriter.WriteLine(xmlReader.Value);
}
}
return textWriter.ToString();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With