472,127 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

use XmlReader/XmlWriter to reformat XML?

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
Nov 12 '05 #1
3 7937
Michael Malinak wrote:
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.


You missed XmlWriter.WriteNode() method.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
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

"Oleg Tkachenko" <oleg@!no!spam!please!tkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Michael Malinak wrote:
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.


You missed XmlWriter.WriteNode() method.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com

Nov 12 '05 #3
Michael Malinak wrote:
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.


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
http://blog.tkachenko.com
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Shivonne | last post: by
1 post views Thread by Fede | last post: by
1 post views Thread by Philipp Schumann | last post: by
8 posts views Thread by Charles.Deisler | last post: by
2 posts views Thread by muesliflakes | last post: by
2 posts views Thread by Amit | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.