My first question is here, so bear with me. Basically, my problem is this: I am creating an XML IDE for the internal language. Its feature should be automatic XML indentation with some command. Like in Visual Studio, etc.
Basically I need to include 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>
IN:
<?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>
This is the indent - but do not touch anything. Is it possible in C # without writing an algorithm from scratch, i.e. with LINQ XDocument or with some XmlWriter implementations?
I have tried the following so far (from What is the easiest way to get deferred 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 line breaks 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 who has comments or answers.