logo       

Re: Writing XML data to a file: msg#00006

Subject: Re: Writing XML data to a file
Thanks, Rick,

I decided to go ahead and re-write the whole file, since it isn't big 
(less than 3k).  Basically, what I do is find my root node 
("Archives") in my XML dom and remove all child nodes (all 
the "Archive" nodes) with a AchivesNode.RemoveAll().  Then just go 
through my table and build a new Archive node (which is actually an 
XmlElement) and add the 3 elements that I need, add them to the  
newly created Archive child node and then add that to the Archives 
node.  Once I'm done with that, I delete the existing XML file and 
rewrite it with the doc.Save method.

The one thing to remember here is for each Element that you create, 
you add the text by using the InnterText rather than the Value 
property of the Element.

Hope this helps someone else out there!

D

--- In padnug@xxxxxxxxxxxxxxx, "Rick Strahl" <rstrahl@xxxx> wrote:
> Keith,
> 
> There are a number of ways to accomplish this. The easiest way, if 
the XML
> document is not huge is the the XmlDocument object to load the XML 
into it,
> find the location where you want to insert elements (either by 
parsing the
> ChildNodes tree or by using SelectSingleNode()) and then simply 
inserting a
> new XML Node with the data you need. This is pretty easy to do.
> 
> If the file is large (depends on your environment really - so 
test)  you may
> have to use an XmlReader and XmlWriter to read the content of the 
old XMl
> doducment and then re-write the document as you go, adding the new 
content
> as needed.
> 
> XmlDom is pretty simple to do - you should be able to find a sample 
online
> easily. XmlReader/Writer is a little more non-obvious <g> and 
definitely
> more work.
> 
> As for good books entry level books on XML I like Dan Wahlin's Xml 
for
> ASP.NET developers which hits all the highlights in a very readable 
and
> smallish book.
> 
> http://www.amazon.com/exec/obidos/ASIN/0672320398/westwindtechn-
20/002-94615
> 63-3270443
> 
> Dino Esposito has another one that's more complete and goes a 
little deeper
> as well as dealing more with data centric issues:
> http://www.amazon.com/exec/obidos/ASIN/0735618011/westwindtechn-
20/002-94615
> 63-3270443
> 
> I have and use both frequently...
> 
> +++ Rick ---
> 
> Rick Strahl
> West Wind Technologies
> http://www.west-wind.com/
> http://www.west-wind.com/weblog
> http://www.west-wind.com/wwthreads
> ----------------------------------
> Making waves on the Web 
> 
> 
>  
> 
> > -----Original Message-----
> > From: padnug@xxxxxxxxxxxxxxx [mailto:padnug@xxxxxxxxxxxxxxx] 
> > On Behalf Of Keith R. Pinster
> > Sent: Tuesday, August 02, 2005 10:44 AM
> > To: padnug@xxxxxxxxxxxxxxx
> > Subject: [padnug] Writing XML data to a file
> > 
> > I was wondering if anyone can point me to an article or book 
> > that tells me how to write individual XML nodes into the 
> > middle of a file.  
> > 
> > Here's what I've got: I've got an XML file with several 
> > "tables" that are used to configure a web page.  I want to 
> > take 1 of these "tables", modify the data and then write it 
> > back out to the file.  The XML file may be modified (sections 
> > added or deleted) and my little config app is being written 
> > for only 1 specific section.  I'm trying to avoid reading the 
> > entire XML file in and then writing it to file from scratch.
> > 
> > Is there a way to do this?
> > 
> > 
> > 
> > 
> > 
> > ------------------------ Yahoo! Groups Sponsor 
> > --------------------~--> <font face=arial size=-1><a 
> > href="http://us.ard.yahoo.com/SIG=12heu5cgu/M=362131.6882499.7
> > 825260.1510227/D=groups/S=1705007709:TM/Y=YAHOO/EXP=1123011906
> > /A=2889191/R=0/SIG=10r90krvo/*http://www.thebeehive.org
> > ">Get Bzzzy! (real tools to help you find a job) Welcome to 
> > the Sweet Life - brought to you by One Economy</a>.</font> 
> > --------------------------------------------------------------
> > ------~-> 
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> >




------------------------ Yahoo! Groups Sponsor --------------------~--> 
<font face=arial size=-1><a 
href="http://us.ard.yahoo.com/SIG=12hhih3dp/M=362131.6882499.7825260.1510227/D=groups/S=1705007709:TM/Y=YAHOO/EXP=1123020656/A=2889191/R=0/SIG=10r90krvo/*http://www.thebeehive.org
">Get Bzzzy! (real tools to help you find a job) Welcome to the Sweet Life 
- brought to you by One Economy</a>.</font>
--------------------------------------------------------------------~-> 

 


<Prev in Thread] Current Thread [Next in Thread>