473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 8009
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shivonne | last post by:
Hi all Having problems with the XmlReader object. I'm trying to transform an XML document with XSLT - and get the resulting output. My code is as follows: XsltArgumentList xslArgs =...
1
by: Fede | last post by:
Hi to all! someone of you know the difference between XMLWriter/XMLReader and MSXML4.0? The first one has the same capacity of the second one? Which is better to use? Which give the clearest...
1
by: Philipp Schumann | last post by:
Hi, I have the following code to copy nodes from an XML document (XmlReader reader) to some output (XmlWriter writer). while (reader.Read ()) if (reader.MoveToContent () ==...
8
by: Charles.Deisler | last post by:
Im currently using the following code.. XmlDocument xmlData = new XmlDocument(); XmlTextWriter xmlwriter = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8); XmlReader...
2
by: muesliflakes | last post by:
I wish to receive some data from SqlServer as XML and write out result to as a string. Eg: cmd = new SqlCommand( "select * from blah for xml auto", con ); XmlReader result =...
2
by: Jeff Calico | last post by:
Hello all. I am implementing a SAX filter to strip a bunch of unneeded elements out of a large XML file. I found a book "Java & XML" by Brett McLaughlin, and an interesting article by him wich...
2
by: Amit | last post by:
I have assigned a simple xml into a XmlReader. A switch case loops through the nodes and writes the data to XmlWriter object which is initialized to a xml file. The output xml file is correctly...
0
by: jrd | last post by:
Hello, I am very new to PHP. What I need to do is read an XML file (RSS feed) into memory, iterate through the file changing one particular value for each entry, and write a new XML file with a...
2
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.