473,399 Members | 2,774 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,399 software developers and data experts.

Write XML to File while reading

I'm reading in an XML file from the server using
XmlTextReader in C# like so:

XmlTextReader xr = new XmlTextReader(url);

while(xr.Read())

{

//parse the xml file here

}

Now then, I need to also save this file to the local file

system and I should be able to do this at the same time

I'm reading the file with XmlTextReader, right?

I figured I don't really need to use XMLTextWriter since

the file is already in XML format, so I tried StreamWriter

and then tried to write the file in the while loop one line at

a time using 'streamwriter.write(xr.Name)' but that's not quite

right.

Thing is, how do I write a whole line of XML back to the file?

Is there a better way to do this that I'm not thinking of?

Thanks,

Drew


Nov 12 '05 #1
4 5526
Drew wrote:
Now then, I need to also save this file to the local file

system and I should be able to do this at the same time

I'm reading the file with XmlTextReader, right?


Use XmlTextWriter and XmltextWriter.WriteNode(XmlReader) method to write
out a node XmlReader is positioned at.

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
> Use XmlTextWriter and XmltextWriter.WriteNode(XmlReader) method to write
out a node XmlReader is positioned at.


Hmmm...that worked, but now the parsing is all messed up.

I assume this is because the WriteNode method moves the pointer?

Maybe I wasn't parsing the information correctly in the first place?

I was doing something like this in the loop:
if(xr.Name.Equals("country"))

{

// store parsed variables

xw.WriteNode(xr,false);

}

Any more ideas?


Nov 12 '05 #3
The WriteNode method does drive the reader to jump over the entire node, it
means that you are not going to receive any Read events.

If you parser every single node, it does make sence to use the XmlWriter to
persist the output result.

Howard Hao

"Drew" <so*****@hotmail.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
Use XmlTextWriter and XmltextWriter.WriteNode(XmlReader) method to write
out a node XmlReader is positioned at.


Hmmm...that worked, but now the parsing is all messed up.

I assume this is because the WriteNode method moves the pointer?

Maybe I wasn't parsing the information correctly in the first place?

I was doing something like this in the loop:
if(xr.Name.Equals("country"))

{

// store parsed variables

xw.WriteNode(xr,false);

}

Any more ideas?

Nov 12 '05 #4
Drew wrote:
Hmmm...that worked, but now the parsing is all messed up.

I assume this is because the WriteNode method moves the pointer?


WriteNode writes out node along with all descendant nodes - it's deep
write. So use it on nodes you want to write out as is. For others - use
WriteXXX methods.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #5

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

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
4
by: apriebe47 | last post by:
Alright, I realize this is probably very basic to be posted on this newsgroup but I cannot figure out what is causing my problem. Here is the code I am using below: from getpass import getpass ...
16
by: btopenworld | last post by:
Hi - question from a relative asp novice I have written to text files in the past, but always appending new data to the end of the text file. I now want to add the new data to the beginning of...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
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
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...

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.