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.Inde nted.
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.Create Node(XmlNodeTyp e.XmlDeclaratio n,"","");
document.Append Child(newNode);
// Add a root element
XmlElement newElement = document.Create Element("DemoXm lDocument");
XmlText newElementText = document.Create TextNode("This is the text of
the root element");
newElement.Appe ndChild(newElem entText);
document.Append Child(newElemen t);
// Get the root element
XmlElement rootElement = document.Docume ntElement;
// Add a child element
newElement = document.Create Element("Sample Element");
newElementText = document.Create TextNode("The text of the sample
element");
newElement.Appe ndChild(newElem entText);
rootElement.App endChild(newEle ment);
// Save the XML document to file
string fileName = @"c:\temp\DemoD ocument.xml";
XmlTextWriter writer = new XmlTextWriter(f ileName, Encoding.Defaul t);
writer.Formatti ng = Formatting.Inde nted;
document.Save(w riter);
writer.Close();
}
Here's what the output from the program looks like:
<?xml version="1.0"?>
<DemoXmlDocumen t>This is the text of the root element<SampleE lement>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.
--
David Veeneman
Foresight Systems
line breaks and indentation are missing, even though I'm using an
XmlTextWriter with Formatting = Formatting.Inde nted.
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.Create Node(XmlNodeTyp e.XmlDeclaratio n,"","");
document.Append Child(newNode);
// Add a root element
XmlElement newElement = document.Create Element("DemoXm lDocument");
XmlText newElementText = document.Create TextNode("This is the text of
the root element");
newElement.Appe ndChild(newElem entText);
document.Append Child(newElemen t);
// Get the root element
XmlElement rootElement = document.Docume ntElement;
// Add a child element
newElement = document.Create Element("Sample Element");
newElementText = document.Create TextNode("The text of the sample
element");
newElement.Appe ndChild(newElem entText);
rootElement.App endChild(newEle ment);
// Save the XML document to file
string fileName = @"c:\temp\DemoD ocument.xml";
XmlTextWriter writer = new XmlTextWriter(f ileName, Encoding.Defaul t);
writer.Formatti ng = Formatting.Inde nted;
document.Save(w riter);
writer.Close();
}
Here's what the output from the program looks like:
<?xml version="1.0"?>
<DemoXmlDocumen t>This is the text of the root element<SampleE lement>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.
--
David Veeneman
Foresight Systems
Comment