Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append data to an xml file

Status
Not open for further replies.

shabnam

Programmer
Oct 3, 2000
26
0
0
US
I need to append data to a pre-existing xml file.

We run a process and write out a certain amount of information in the beginning. The we need to append data to the file as long as the process is running.

My file will look something like this

<Main Tag>
<BeginningData value="This is the data added in the beginning"/>
<Items>
<Item description="Data incrementatlly added" value = "1"/>
<Item description="Data incrementatlly added" value = "2"/>
</Items>
</Main Tag>

The problems I have are these -

1. I don't know a way to write the information out so that the xml file is NOT well formed. If I can write it out so that the ending tag (ie if my file does NOT have </Main Tag>) I can just write out the incremental data and then at the end of the process - write out the ending tag.

2. If the ending tag exists - how do I write out the incremental data. I don't want to use the XmlDocument object to load the file, add the data and then re-write the entire object out because these files get really big. The only other solution I know of is to use the XmlTextReader and XmlTextWriter - seek to the end of the file - find the beginning of the end tag and then overwrite it. This solution is a big brittle and I don't want to use it if possible.

Thanks
 
You'll have to read the file and rewrite it until you find the ending tag, then write out your data, then write out the ending tag. That means that you'll need TWO files. That would be a big pain. Or you could store the input in an array, close the file, reopen it for output, and rewrite it from the array. Another pain. I think the idea of using the XmlDocument object is probably your best bet. It is already there, and meant to do exactly what you want. I doubt having that object read and write the file is significantly less efficient than reading and writing it yourself, and you wouldn't have to fiddle around with two files, or an array.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
If you don't want to use XmlDocument, I'd just treat it as a string. Use string manipulation, just chop of the last however many characters and insert the new string on the end.

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top