473,409 Members | 1,983 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,409 software developers and data experts.

Why output of WriteNode is not in XML format?

Hi.
I got some sample codes from Fig 10 of
http://msdn.microsoft.com/msdnmag/is...L/default.aspx
..
If I read an XML file like:
<test>
<good>1</good>
</test>
The output becomes:
<test>
1
</test>
Why WriteNode dropped the "good" tag?
I want to read an XML file and output an XML file.
Please tell me how to use the following codes.(I modified it a little
bit)
Thank you,
-----------------------------------------------------------------
using System;
using System.Xml;
using System.IO;
using System.Xml.Schema;
// ...
XmlTextReader reader = new XmlTextReader(@"c:\test.xml");
XmlTextWriter writer = new XmlTextWriter(Console.Out);

// Configure reader and writer
writer.Formatting = Formatting.Indented;
reader.MoveToContent();

// Write the root
writer.WriteStartElement(reader.LocalName);

// Read and output every other node
int i=0;
while(reader.Read())
{
// if (i % 2)
writer.WriteNode(reader, false);
i++;
}

// Close the root
writer.WriteEndElement();

// Close reader and writer
writer.Close();
reader.Close();
Nov 12 '05 #1
5 2764
Frank Meng wrote:
I got some sample codes from Fig 10 of
http://msdn.microsoft.com/msdnmag/is...L/default.aspx
.
If I read an XML file like:
<test>
<good>1</good>
</test>
The output becomes:
<test>
1
</test> // Read and output every other node

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note above.

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
As I wrote in my first message, I modified the codes.
I commented out line "// if (i % 2)". The new codes should print
out every line.
Also, the program didn't skip the line, it only dropped the tag and
printed the value.

Frank

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<ed**************@TK2MSFTNGP10.phx.gbl>...
Frank Meng wrote:
I got some sample codes from Fig 10 of
http://msdn.microsoft.com/msdnmag/is...L/default.aspx
.
If I read an XML file like:
<test>
<good>1</good>
</test>
The output becomes:
<test>
1
</test>

// Read and output every other node

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note above.

Nov 12 '05 #3
Frank Meng wrote:
As I wrote in my first message, I modified the codes.
I commented out line "// if (i % 2)". The new codes should print
out every line.


Well, then there is something wrong in your code. Actually the pattern
of writing all XMlReader reads as is is as simple as

XmlTextReader r = new XmlTextReader("foo.xml");
XmlTextWriter w = new XmlTextWriter(Console.Out);
while (r.Read())
w.WriteNode(r, false);
r.Close();
w.Close();

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #4
Thank you for your message.
After I added
reader.WhitespaceHandling = WhitespaceHandling.None;
, the program will output in XML format.
But it won't read the file line by line.
Without the above line, or with
reader.WhitespaceHandling = WhitespaceHandling.All;
, the program will read the file line by line, but the output drops
all XML tags like I mentioned before.
I want to read the information, process the information and append new
data to the XML file.
So I want to read it line by line so I can get the information and
output in XML format.
Please let me know how I can do it.
Frank

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<u$**************@TK2MSFTNGP10.phx.gbl>...
Frank Meng wrote:
As I wrote in my first message, I modified the codes.
I commented out line "// if (i % 2)". The new codes should print
out every line.


Well, then there is something wrong in your code. Actually the pattern
of writing all XMlReader reads as is is as simple as

XmlTextReader r = new XmlTextReader("foo.xml");
XmlTextWriter w = new XmlTextWriter(Console.Out);
while (r.Read())
w.WriteNode(r, false);
r.Close();
w.Close();

Nov 12 '05 #5
Frank Meng wrote:
After I added
reader.WhitespaceHandling = WhitespaceHandling.None;
, the program will output in XML format.
But it won't read the file line by line.
What do you mean? XmlReader doesn't read line by line. It reads node by
node.
Without the above line, or with
reader.WhitespaceHandling = WhitespaceHandling.All;
, the program will read the file line by line, but the output drops
all XML tags like I mentioned before.


Sorry, I cannot reproduce the problem. Provided input XML is
<test>
<good>1</good>
</test>

the code:

XmlTextReader r = new XmlTextReader("foo.xml");
r.WhitespaceHandling = WhitespaceHandling.All;
XmlTextWriter w = new XmlTextWriter(Console.Out);
while (r.Read())
w.WriteNode(r, false);
r.Close();
w.Close();

outputs

<test>
<good>1</good>
</test>

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sebastien de Menten | last post by:
Hi, I am using the profile module to watch hot spots in a python application. The default output is already useful but has anyone tried to produce output readable by kcachegrind...
0
by: shaun | last post by:
Xerces-C++ appears to have different methods for 1) Output to file and 2)Output to screen, namely (cut and paste from code): LocalFileFormatTarget target(X(fileName.c_str()));...
2
by: Andras Belokosztolszki | last post by:
I'm having a problem with XmlTextWriter.WriteNode. I have a document with a specific element repeating. say <root><a></a><a></a></root> I iterate through this with an XmlTextReader, I filter out...
4
by: newbiecpp | last post by:
How can I format output string. For example, when I use: Console.WriteLine("12345") I want to output like " 12345", i.e., I need set width is 10 and align at right. How can I do it. ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
1
by: nriley | last post by:
Hi, I've got a number of doctests which rely on a certain output format, and since I wrote the tests I've changed the output format. Now, almost all the tests fail. What I'd like is if I...
2
by: johnperl | last post by:
i am working on the script as below, i want an output according to the format. i am trying to put everything in one loop but cannot figure out the way to do so. could anyone please help me with this....
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
8
by: victor.herasme | last post by:
Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.