D
David Veeneman
I'm having a problem getting XML output properly formatted. specifically,
line breaks and indentation are missing, even though I'm using an
XmlTextWriter with Formatting = Formatting.Indented.
Here is the code that's generating the problem:
static void Main(string[] args)
{
// Create an XML document
XmlDocument document = new XmlDocument();
// Add an XML declaration section
XmlNode newNode = document.CreateNode(XmlNodeType.XmlDeclaration,"","");
document.AppendChild(newNode);
// Add a root element
XmlElement newElement = document.CreateElement("DemoXmlDocument");
XmlText newElementText = document.CreateTextNode("This is the text of
the root element");
newElement.AppendChild(newElementText);
document.AppendChild(newElement);
// Get the root element
XmlElement rootElement = document.DocumentElement;
// Add a child element
newElement = document.CreateElement("SampleElement");
newElementText = document.CreateTextNode("The text of the sample
element");
newElement.AppendChild(newElementText);
rootElement.AppendChild(newElement);
// Save the XML document to file
string fileName = @"c:\temp\DemoDocument.xml";
XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.Default);
writer.Formatting = Formatting.Indented;
document.Save(writer);
writer.Close();
}
Here's what the output from the program looks like:
<?xml version="1.0"?>
<DemoXmlDocument>This is the text of the root element<SampleElement>The text
of the sample element</SampleElement></DemoXmlDocument>
The content is fine, but the formatting is a mess. Any ideas why? Thanks for
your help.
line breaks and indentation are missing, even though I'm using an
XmlTextWriter with Formatting = Formatting.Indented.
Here is the code that's generating the problem:
static void Main(string[] args)
{
// Create an XML document
XmlDocument document = new XmlDocument();
// Add an XML declaration section
XmlNode newNode = document.CreateNode(XmlNodeType.XmlDeclaration,"","");
document.AppendChild(newNode);
// Add a root element
XmlElement newElement = document.CreateElement("DemoXmlDocument");
XmlText newElementText = document.CreateTextNode("This is the text of
the root element");
newElement.AppendChild(newElementText);
document.AppendChild(newElement);
// Get the root element
XmlElement rootElement = document.DocumentElement;
// Add a child element
newElement = document.CreateElement("SampleElement");
newElementText = document.CreateTextNode("The text of the sample
element");
newElement.AppendChild(newElementText);
rootElement.AppendChild(newElement);
// Save the XML document to file
string fileName = @"c:\temp\DemoDocument.xml";
XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.Default);
writer.Formatting = Formatting.Indented;
document.Save(writer);
writer.Close();
}
Here's what the output from the program looks like:
<?xml version="1.0"?>
<DemoXmlDocument>This is the text of the root element<SampleElement>The text
of the sample element</SampleElement></DemoXmlDocument>
The content is fine, but the formatting is a mess. Any ideas why? Thanks for
your help.