I'm trying to write a new line after xml.WriteStartDocument()
. When I do it throws an exception. It doesn't appear to throw an exception if I write it within the root element.
InvalidOperationException: Token Text in state Document would result in an invalid XML document.
So why does this not work? And how can I make it work?
using(XmlWriter xml = XmlWriter.Create(xmlFile))
{
xml.WriteStartDocument();
xml.WriteString("\n"); // <-- Exception
// more nodes here
}
I'm trying to output new line between XML declaration and root node, as without it XmlWriter produces one single hard to read string. Desired output:
<?xml version="1.0" encoding="utf-8"?>
<root/>