My first question here, so bear with me. Basically, my problem is this: Im building an XML IDE for an internal language. A feature of it should be to auto-indent the XML by using some command. Similar to what is found in Visual Studio etc.
Basically what I need is to turn the following Xml:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Into:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
That is indent - but touch nothing else. Is this possible in C# without writing an algorithm from scratch, i.e., with LINQ XDocument or some XmlWriter implementaion?
I've tried the following so far (from What is the simplest way to get indented XML with line breaks from XmlDocument?)
static public string Beautify(this XmlDocument doc)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(sb, settings)) {
doc.Save(writer);
}
return sb.ToString();
}
But this removes linebreaks and gives me:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Thanks in advance to anyone with comments or answers.