I was recent faced with writing code to generate an xml file to be sent to an other company that automaticly processes it. No big deal, I thought, just use the XmlTextWriter, flip through my data and generate the file. All well and good. Then the other company came back and said they couldn't handle the xml being all on one line, they needed a new line for each tag. So after a bit of research I found that the XmlTextWriter has some formatting properties that can be used to create indenting, new lines and such. The code I used is this.
//initialize the Xml Text Writer
XmlOutput = new XmlTextWriter(m_memStream, Encoding.UTF8);
XmlOutput.Formatting = Formatting.Indented;
XmlOutput.Indentation = 0;
XmlOutput.IndentChar = '\n';
The one thing I want to note is that in the examples I saw, the Indentation property was used to say how many IndentChar that you want, for example, 5 spaces. So, I set this to 1 thinking that I would get one new line character, but for some reason, I got 3 or 4 new lines. Moral of the story is, if you are using the Indentation format for the XmlTextWriter and you are trying to get a new line, set the Indentaion property to zero.
Superb info thanks. I was generating SQL dynamically and DOS has a char limit so I need new lines in the output. This worked a treat.
Posted by: Dave | December 12, 2008 at 02:06 AM