private static string getValue(Manager mgr, Document document, 
		string query, QueryContext context)
    {
        ///////////////////////////////////////////////////////////////////////////
        ////////    Return specific information from a document. //////////////////
        ////////          Assumes a result set of size 1         //////////////////
        ///////////////////////////////////////////////////////////////////////////

        using(Value docValue = new Value(document))
        {
            // Perform the query
            // The document provides the context for the query, so neither
            // collection() nor doc() needs to be part of the query.
            using(QueryExpression queryExpr = mgr.Prepare(null, query, context))
            {
                using(Results result = queryExpr.Execute(null, docValue, context, new DocumentConfig()))
                {
                    // We require a result set size of 1.
                    if(!result.MoveNext())
                    {
                        System.Console.WriteLine("Error!  query '" + query +
                            "' returned a result size size != 1");
                        throw new System.Exception( "getValue found result set not equal to 1.");
                    }

                    // Get the value. If we allowed the result set to be larger than size 1,
                    // we would have to loop through the results, processing each as is
                    // required by our application.
                    return result.Current.ToString();
                }
            }
        }
    }
Exemple #2
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);
                        }
                    }
                }
            }
        }
    }