473,856 Members | 1,703 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 8055
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
1343
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
2623
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
1802
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
2494
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
17212
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
2232
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
3907
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
1738
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
5607
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
9903
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
11048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10770
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
9526
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...
0
7084
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();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4567
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
4168
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.