473,689 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8038
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.Write Node() 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.Creat e( streamContent );
reader.MoveToCo ntent();
StringBuilder sb = new StringBuilder() ;
XmlWriterSettin gs settings = new XmlWriterSettin gs();
settings.Indent = true;
settings.Indent Chars = ("\t");
settings.Encodi ng = System.Text.Enc oding.UTF8;
XmlWriter writer = XmlWriter.Creat e( sb, settings );
writer.WriteNod e( 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.NewLin eOnAttributes = true, it does put attributes on a
new line.

Am I again missing something simple?

Michael

"Oleg Tkachenko" <oleg@!no!spam! please!tkachenk o.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.Write Node() 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.Creat e( streamContent );
reader.MoveToCo ntent();
StringBuilder sb = new StringBuilder() ;
XmlWriterSettin gs settings = new XmlWriterSettin gs();
settings.Indent = true;
settings.Indent Chars = ("\t");
settings.Encodi ng = System.Text.Enc oding.UTF8;
XmlWriter writer = XmlWriter.Creat e( sb, settings );
writer.WriteNod e( 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.NewLin eOnAttributes = 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
1326
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 = passParameters (xmlMenu.SelectSingleNode("document/setup")); // just a local function to get all parameters in the doc XslTransform xslTransform = new XslTransform();
1
2619
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 messages or the greatest amount of information about the kind and the origin of error (considering that I have to show messages in Italian and the Framework is in English)? And ...is it difficult to include msxml4.dll in my project? Thank you in...
1
1796
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 () == XmlNodeType.Element) break; // forward the reader to the document element writer.WriteStartDocument (false);
8
2488
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 xmlreader = SqlHelper.ExecuteXmlReader(...) xmlData.Load(xmlreader); xmlData.WriteTo(xmlwriter); xmlwriter.Flush();
2
17186
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 = cmd.ExecuteXmlReader( ); Console.Writeln( result.ToString( ) ); This XML is meant to be the input for an XSLT transform so I'm
2
2223
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 address my issues: http://www-128.ibm.com/developerworks/xml/library/x-tipbigdoc3.html However, doing it in that specified way does not seem to work at all! Doing it very differently *seems* to work, but most likely I am not understanding...
2
3902
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 generated and everything works fine. But if the input xml has an &amp; then I am unable to generate a valid output xml file. I have pasted the code below: public class Class1 {
0
1735
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 different name. I have a pretty good understanding of how to use xmlreader to read the XML file and get at the values for each item in the feed. What I'm wondering is, can I use xmlwriter simultaneously with xmlreader to modify one of the...
2
5587
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 bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
8598
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8531
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8786
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8793
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6457
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.