Groups | Blog | Home
all groups > dotnet xml > august 2005 >

dotnet xml : use XmlReader/XmlWriter to reformat XML?


Oleg Tkachenko
8/19/2005 12:00:00 AM
[quoted text, click to view]

You missed XmlWriter.WriteNode() method.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
Michael Malinak
8/19/2005 8:25:06 AM
Since XmlWriter offers so many nice options for formatting, I thought it
would be nice to read in via XmlReader, and write back out via XmlWriter.
It might be overkill, but I'd also like to be able to check some values
during that time also so I was going to be using XmlReader anyway.
Unfortunately I don't see an easy way to stream it back out through
XmlWriter without going node by node.

Any suggestions? Is there an easier/faster way to do this already?

Michael

Michael Malinak
8/19/2005 1:49:20 PM
Thank you, I completely missed the version that took a reader.

I am seeing some other strangeness with this code:

XmlReader reader = XmlReader.Create( streamContent );
reader.MoveToContent();
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("\t");
settings.Encoding = System.Text.Encoding.UTF8;
XmlWriter writer = XmlWriter.Create( sb, settings );
writer.WriteNode( reader, true );
writer.Close();

I specifically set the encoding to be UTF-8 and to indent the content, but
the header comes out as UTF-16 encoding and none of the contenet is tabbed.
If I set settings.NewLineOnAttributes = true, it does put attributes on a
new line.

Am I again missing something simple?

Michael

[quoted text, click to view]

Oleg Tkachenko [MVP]
8/21/2005 11:18:09 AM
[quoted text, click to view]

Strings in .NET are always UTF-16 encoded, that's why you get UTF-16.
And I can't reproduce the problem with indenting, it works for me.
Chances are your XML has mixed content (e.g. "Some <b>text</b> and
<i>more</i> text"), then formatting cannot be done as it would break
significant document data.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
AddThis Social Bookmark Button