Figaro - The XML Database for the .NET Framework

You can either replace a document in its entirety as described here, or you can modify just portions of the document as described in Modifying XML Documents.

If you already have code in place to perform document modifications, then replacement is the easiest mechanism to implement. However, replacement requires that at least the entire replacement document be held in memory. Modification, on the other hand, only requires that the portion of the document to be modified be held in memory. Depending on the size of your documents, modification may prove to be significantly faster and less costly to operate.

You can directly replace a document that exists in a container. To do this:

  1. Retrieve the document from the container. Either do this using an XQuery query and iterating through the results set looking for the document that you want to replace, or use GetDocument to retrieve the document by its name. Either way, make sure you have the document as an XmlDocument object.
  2. Use SetContent or SetContentAsInputStream to set the object's content to the desired value.
  3. Use UpdateDocument to save the modified document back to the container.
Alternatively, you can create a new blank document using CreateDocument, set the document's name to be identical to a document already existing in the container, set the document's content to the desired content, then call UpdateDocument.
CopyC#
using System;
using System.Diagnostics;
using System.IO;
using Figaro.BerkeleyDB.Xml;            
namespace Figaro.Documentation.Examples
{
    class ReplacingDocuments
    {
        private const string baseUri = @"C:\dev\db\";
        private const string testdb = "ReplacingDocuments.dbxml";
        private const string testdata = @"C:\dev\db\xmlData\nsData\";

        static void Main()
        {
            //get our manager
            using (var mgr = new XmlManager(ManagerInitOptions.AllowExternalAccess | ManagerInitOptions.AllowAutoOpen))
            {
                prepareContainer(true);
                //open a container
                using (var container = mgr.OpenContainer(baseUri + testdb))
                {
                    try
                    {
                        Console.WriteLine("container has {0} documents.", container.GetNumDocuments());
                        // add a container alias if your container is not in 
                        // the same directory as your application
                        container.AddAlias("testdb");
                        // get a query context
                        UpdateContext updateContext = mgr.CreateUpdateContext();
                        const string docName = "Bananas.xml";            
                        XmlDocument bananaDoc = container.GetDocument(docName);
                        bananaDoc.SetContent("<products><product>New Bananas!</product></products>");
                        container.UpdateDocument(bananaDoc, updateContext);                        
                    }
                    finally
                    {
                        container.Close();
                    }
                }
            }
        }