[Mono-list] Xml reading & writing.

Neil Munro 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:
>    http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
>    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:
>    http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
>    http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx
>    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
>
> http://msdn.microsoft.com/en-us/library/system.xml.xpath.ixpathnavigable.aspx
>    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...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080917/3b1f8548/attachment.html 


More information about the Mono-list mailing list