[Mono-list] Xml reading & writing.

Jonathan Pryor jonpryor at vt.edu
Tue Sep 16 22:12:11 EDT 2008


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.

  * 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




More information about the Mono-list mailing list