Sunday, July 09, 2006

 
Updating an XML File

This is very easy to do, albeit not necessarily efficient. All you need is a XMLDocument object, then navigate the DOM to retrieve whatever nodes you want to update and then save the file. So let's say, for instance, that I want to update a section of my file which has text for my home page. Here is how you would do it:


Vb.Net

'load a XMLDocument object with the appropriate file
Dim doc as new XMLDocument("mySite.xml")
'retrieve the part that you want to update.
Dim homePage as XMLNode = doc.selectSingleNode("/mySite/home")
'do the update
homePage.InnerText = "This is the updated text"
'and now save the changes
doc.Save()


You have to be careful though when using XML like this, mostly with very large files, because, as you can see, while it's easy enough to get to specific sections of the file and update them using .Net you not only have to load the whole file in memory in order to find the piece of the file that you want,but also when you use Save() it actually saves the whole file again.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?