예제 #1
0
    // 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);
                        }
                    }
                }
            }
        }
    }