473,799 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(u rl);

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.w rite(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 5550
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.W riteNode(XmlRea der) 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.W riteNode(XmlRea der) 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.Equa ls("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*****@hotmai l.com> wrote in message
news:e3******** ******@TK2MSFTN GP09.phx.gbl...
Use XmlTextWriter and XmltextWriter.W riteNode(XmlRea der) 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.Equa ls("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
3026
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* file, not for the *remaining* data to be written. It happens both under 2.3 and 2.2.3. Any ideas?
1
6771
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 CLogHandler() { this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); this.m_swLog = new StreamWriter(this.m_fsLog);
1
4311
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 time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
5
5120
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 Partition we are using ReadFile , WriteFile and setFilePointerEx to read and write the sectors and we are reading/writing 102400 sectors together, even we have reduced the sectors still it is not able to read or write, our program is working fine...
4
1850
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 configfile = file('config.txt', 'w') serverPassword = configfile.readline() if serverPassword == '': print "Server warning: No password has been set!"
16
2996
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 the file. The text file would be a record of orders placed through a web form. The text file would merely be a record of the orders but it needs to be in descending date order - each new record added to the beginning.
6
249001
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 System; using System.IO; using System.Diagnostics;
63
3268
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 mean=0,linput=0; FILE *fpr, *fpw;
1
3930
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 of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
9687
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
9543
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,...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10029
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
9077
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
7567
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
6808
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
4144
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
3761
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.