Re: Create xml with formatting

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Thu, 25 Aug 2005 00:41:02 -0700, cnu wrote:

Hi!

> System.Xml.XmlTextWriter xtw = new XmlTextWriter(aFilePath, null);
> xtw.Formatting = Formatting.Indented;
> xmlDoc.Save(xtw);
> xtw.Flush();
> xtw.Close();

It looks like you have found a bug in .NET XmlWriter :/
The code below seems to proove it... Any MVP here?

// --- the code ---
System.Xml.XmlTextWriter xtw = new XmlTextWriter(@"c:\temp\aTest.xml", System.Text.Encoding.UTF8);
xtw.Formatting = Formatting.Indented;

xtw.WriteStartElement("Message");
xtw.WriteStartElement("Parent");
xtw.WriteAttributeString("Index", "0");
xtw.WriteAttributeString("Length", "6");
xtw.WriteString("123"); // get rid of this line and everything is ok

xtw.WriteStartElement("Child1");
xtw.WriteAttributeString("index", "0,1");
xtw.WriteAttributeString("length", "6");
xtw.WriteString("abcd");

xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();

xtw.Flush();
xtw.Close();
// --- end ---

Cheers,
Piotrek

.



Relevant Pages