Beispiel #1
0
        /// <summary>
        /// Demonstrate XQuery Update
        /// </summary>

        public override void run(Uri samplesDir)
        {
            Processor processor = new Processor(true);

            DocumentBuilder loader = processor.NewDocumentBuilder();
            loader.BaseUri = new Uri(samplesDir, "data/books.xml");
            loader.TreeModel = TreeModel.LinkedTree;
            XdmNode indoc = loader.Build(new Uri(samplesDir, "data/books.xml"));

            Console.Out.WriteLine("=========== BEFORE UPDATE ===========");

            Serializer serializer0 = new Serializer();
            serializer0.SetOutputProperty(Serializer.METHOD, "xml");
            serializer0.SetOutputProperty(Serializer.INDENT, "yes");
            serializer0.SetOutputWriter(Console.Out);
            processor.WriteXdmValue(indoc, serializer0);

            String query =
                "for $i in //PRICE return \n" +
                "replace value of node $i with $i - 0.05";

            XQueryCompiler compiler = processor.NewXQueryCompiler();
            compiler.UpdatingEnabled = true;
            XQueryExecutable exp = compiler.Compile(query);
            XQueryEvaluator eval = exp.Load();
            eval.ContextItem = indoc;
            XdmNode[] updatedNodes = eval.RunUpdate();
            foreach (XdmNode root in updatedNodes)
            {
                Uri documentUri = root.DocumentUri;
                if (documentUri != null && documentUri.Scheme == "file")
                {
                    Stream stream = UriConnection.getWritableUriStream(documentUri);
                    Serializer serializer = new Serializer();
                    serializer.SetOutputProperty(Serializer.METHOD, "xml");
                    serializer.SetOutputProperty(Serializer.INDENT, "yes");
                    serializer.SetOutputStream(stream);
                    processor.WriteXdmValue(root, serializer);
                }
            }

            Console.Out.WriteLine("=========== AFTER UPDATE ===========");

            processor.WriteXdmValue(indoc, serializer0);
        }