[Mono-list] Xml reading & writing.
neilmunro at gmail.com
Wed Sep 17 17:36:52 EDT 2008
2008/9/17 Jonathan Pryor <jonpryor at vt.edu>
> On Wed, 2008-09-17 at 02:43 +0100, Neil Munro wrote:
> > I have an xml file that is my applications preferences, it's an update
> > tool, now this update tool can update from multiple sources. If no
> > preferences file is found on first run, a default set are saved. Now
> > if the user wishes to add a new update source to the xml file I need
> > to place the <UpdateSource>blah</UpdateSource> in the correct part of
> > the file, since the file holds ALL application preferences, I can't
> > just dump a new source at the end of the text file.
> > I guess my question is, what's the best way to solve this problem?
> As with many things, there are *several* ways to do this:
> * System.Xml.XmlDocument:
> Provides a DOM oriented XML manipulation API.
> Pro: Somewhat easy to use.
> Con: Loads the entire document into memory, so not very useful for
> large documents.
This looks like the solution I am going to go for.
> * System.Xml.XmlReader & System.Xml.XmlWriter:
> Pro: Provides a "pull-model" API so that the entire document need
> not be loaded at once.
> Con: Not as easy to use as XmlDocument.
> * System.Xml.XPath.IXPathNavigable
> Allows reading/writing of XML-like data w/o requiring XML, but
> needs an actual implementation that supports reading/writing,
> which XmlNode (and thus XmlDocument) do.
> And I'm probably missing a few...
> Unless you're working with a large document (read: several MB and
> larger), I'd suggest either XmlDocument or IXPathNavigable for starters.
> - Jon
So I have this:
// Save it in the prefs.xml file.
XmlDocument xmlDoc = new XmlDocument( );
xmlDoc.Load( sPath_To_Prefs_File );
Now I know that the whole document is loaded into memory at this point, how
would i traverse the document in memory to get to the <UpdateSource>
section, read until there are no more of that tag and add a new update
source after the last one and before the next line?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Mono-list