Adding a document to a container is done with the PutDocument method. To do this, you must perform the following steps:
Adding a document to a container
-
Obtain the document you want to put into the container. You can do this with the path and filename of a document residing on the file system, a string variable containing the document contents, or through the Figaro API using XmlDocument and related objects.
-
Provide a name for the document. The document name must be unique or an exception is thrown. You can also request the Container generate a unique name for you by passing GenerateFileName to the method. If you pass a name and use the GenerateFileName parameter, the Container object will use both to create the name.
-
Create an UpdateContext object. This object encapsulates the context within which the container is updated. Reusing the same UpdateContext for a series of puts against the same container can improve your container's write performance.
Note that the content that you supply to Container.PutDocument is read and validated. By default, this includes any schema or DTDs that the document might reference. Since this can cause you some performance issues, you can cause Figaro to only examine the document body itself by passing the WellFormedOnly flag to PutDocument. However, using this flag cause parsing errors if the document references information that might have come from a schema or DTD.
Further, note that while your documents are stored in the container with their shared text entities (if any) as-is, the underlying XML parser does attempt to expand them for indexing purposes. Therefore, you must make sure that any entities contained in your documents are resolvable at load time.
using Figaro.BerkeleyDB.Xml; namespace Figaro.Documentation.Examples { class SampleInputString { private const string testdb = @"C:\dev\db\TestInputString.dbxml"; static void Main(string[] args) { //Instantiate an XmlManager object - and limit its scope using (var mgr = new XmlManager() { DefaultContainerType = XmlContainerType.WholeDocContainer }) { if (mgr.ExistsContainer(testdb)) mgr.RemoveContainer(testdb); //Create a new Container object using (var cont = mgr.CreateContainer(testdb)) { //our sample data, as a string const string sampleData = "<product xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5"><item>Bananas</item><inventory><price>0.55</price><inventory>220</inventory></inventory><vendor>Simply Fresh</vendor></product>"; try { //insert the string into the Container. Create a one-off //update context for the occasion. cont.PutDocument("bananas.xml", sampleData, mgr.CreateUpdateContext(), PutDocumentOptions.None); } finally { cont.Close(); } } } } } }
To load the document from an input stream, the code is identical except that you use the appropriate method on XmlManager to obtain the stream. For example, to load an XmlDocument directly from a file on disk:
using Figaro.BerkeleyDB.Xml; namespace Figaro.Documentation.Examples { class SampleInputStream { private const string testdb = @"C:\dev\db\TestInputStream.dbxml"; static void Main(string[] args) { //Instantiate an XmlManager object - and limit its scope using (var mgr = new XmlManager() { DefaultContainerType = XmlContainerType.WholeDocContainer }) { if (mgr.ExistsContainer(testdb)) mgr.RemoveContainer(testdb); using (var cont = mgr.CreateContainer(testdb)) { //create our local, reusable update context var ctx = mgr.CreateUpdateContext(); /* Approach 1 - create the file stream first */ //grab the file we want to load var inputStream = mgr.CreateLocalFileInputStream(@"C:\dev\db\xmlData\simpleData\Avocado.xml"); //insert the file cont.PutDocument("Avocado.xml", inputStream, ctx, PutDocumentOptions.None); /* Second approach - let the Container grab the file */ cont.PutDocument(@"C:\dev\db\xmlData\simpleData\Artichoke.xml", ctx); //close the container cont.Close(); } } } } }