public static void LoadFiles(Manager mgr, string containerName, FileInfo[] files) { // Open a transactional container ContainerConfig containerconfig = new ContainerConfig(); containerconfig.Create = true; containerconfig.Transactional = true; using(Container container = mgr.OpenContainer(null, containerName, containerconfig)) { // Create an update context using(UpdateContext uc = mgr.CreateUpdateContext()) { // Start a transaction using(Transaction txn = mgr.CreateTransaction()) { DocumentConfig docconfig = new DocumentConfig(); foreach(FileInfo file in files) { using(FileStream stream = file.OpenRead()) { // Create a document using(Document doc = mgr.CreateDocument()) { doc.Name = file.Name; doc.StreamContent = mgr.CreateInputStream(stream); doc.SetMetaData(new MetaData("http://dbxmlExamples/timestamp", "timeStamp", new Value(System.DateTime.Now))); container.PutDocument(txn, doc, uc, docconfig); System.Console.WriteLine("Added " + file.Name + " to container " + containerName); } } } // Commit the index adds txn.Commit(); } } } }
// Method that deletes all documents from a DB XML container that match a given // XQuery. private static void deleteIndex(Manager mgr, Container container, string URI, string nodeName, string indexType, Transaction txn) { System.Console.WriteLine("Deleting index type '" + indexType + "' from node '" + nodeName + "'."); // Retrieve the index specification from the container using(IndexSpecification idxSpec = container.GetIndexSpecification(txn)) { // See what indexes exist on the container int count = 0; System.Console.WriteLine("Before the delete, the following indexes are maintained for the container:"); // Loop over the indexes and report what's there. while(idxSpec.MoveNext()) { System.Console.WriteLine("\tFor node '" + idxSpec.Current.Name + "', found index: '" + idxSpec.Current.Index + "'."); ++count; } System.Console.WriteLine(count + " indexes found."); // Delete the indexes from the specification. idxSpec.DeleteIndex( new IndexSpecification.Entry(URI, nodeName, indexType)); // Get an update context. using(UpdateContext updateContext = mgr.CreateUpdateContext()) { // Set the specification back to the container container.SetIndexSpecification(txn, idxSpec, updateContext); } } // Retrieve the index specification from the container using(IndexSpecification idxSpec = container.GetIndexSpecification(txn)) { // Look at the indexes again to make sure our deletion took. int count = 0; System.Console.WriteLine("After the delete, the following indexes are maintained for the container:"); while(idxSpec.MoveNext()) { System.Console.WriteLine("\tFor node '" + idxSpec.Current.Name + "', found index: '" + idxSpec.Current.Index + "'."); ++count; } System.Console.WriteLine(count + " indexes found."); } }
// Modifies an XML document that is stored in a DB XML container private static void doUpdateDocument(Manager mgr, Container container, string query, QueryContext context, Transaction txn) { System.Console.WriteLine("Updating documents for expression: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // query for all the documents that we want to update Results results = mgr.Query(txn, query, context, new DocumentConfig()); System.Console.WriteLine("Found " + results.Size + " matching the expression '" + query + "'."); // Get an update context. using(UpdateContext updateContext = mgr.CreateUpdateContext()) { while(results.MoveNext()) { Document document = results.Current.ToDocument(); // Retrieve the entire document as a single String object string docString = document.StringContent; System.Console.WriteLine("Updating document: "); System.Console.WriteLine(docString); // This next method just modifies the document string // in a small way. string newDocString = getNewDocument(mgr, document, context, docString); System.Console.WriteLine("Updating document..."); //Set the document's content to be the new document string document.StringContent = newDocString; // Now replace the document in the container container.UpdateDocument(txn, document, updateContext); System.Console.WriteLine("Document updated."); } } }
// Method to add a "description" element after the query's target nodes private static void doModify(Manager mgr, Container container, QueryContext context, string query, Transaction txn) { using(QueryExpression expression = mgr.Prepare(txn, query, context)) { System.Console.WriteLine("Updating document for the expression: '" + query + "' "); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // Print the document(s) to be updated -- those that describe // Zapote Blanco (a fruit). The document strings are only being // printed to show before/after results. // Most modification programs would not perform the additional queries. using(Results results = expression.Execute(txn, context, new DocumentConfig())) { dumpDocuments(results); results.Reset(); System.Console.WriteLine("About to update the document(s) above."); System.Console.WriteLine("Look for a new element after the target " + "element, named 'description' ..."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // The modification is a new element in the target node, called // "descripton, which goes immediately after the <product> element. // if this program is run more than once, and committed, additional // identical elements are added. It is easy to modify this program // to change the modification. using(Modify modify = mgr.CreateModify()) { using(QueryExpression subexpr = mgr.Prepare(txn, ".", context)) { modify.AddInsertAfterStep(subexpr, Modify.XmlObject.Element, "description", "very nice"); using(UpdateContext uc = mgr.CreateUpdateContext()) { long numMod = modify.Execute(txn, results, context, uc); System.Console.WriteLine("Performed " + numMod + " modification operations"); dumpDocuments(results); } } } } } }
// Method that deletes all documents from a DB XML container that match a given // XQuery. private static void doDeleteDocument(Manager mgr, Container container, string query, QueryContext context, Transaction txn) { System.Console.WriteLine("Deleting documents for expression: '" + query + "'."); System.Console.WriteLine("Return to continue: "); System.Console.ReadLine(); // Perform our query. We'll delete any document contained in this result set. Results results = mgr.Query(txn, query, context, new DocumentConfig()); System.Console.WriteLine("Found " + results.Size + " matching the expression '" + query + "'."); // Get an update context. using(UpdateContext updateContext = mgr.CreateUpdateContext()) { while(results.MoveNext()) { Document document = results.Current.ToDocument(); string name = document.Name; System.Console.WriteLine("Deleting document: " + name + "."); // Peform the delete container.DeleteDocument(txn, document, updateContext); System.Console.WriteLine("Deleted document: " + name + "."); } } }