Пример #1
0
        public void DynamicVariable()
        {
            string expr  = "//mvp:titles[mvp:price > 10]";
            string dyn   = "//mvp:titles[mvp:price > $price]";
            int    price = 10;

            XPathNavigator      docnav = Document.CreateNavigator();
            XPathExpression     xpath  = docnav.Compile(expr);
            XmlNamespaceManager mgr    = new XmlNamespaceManager(docnav.NameTable);

            mgr.AddNamespace(Globals.MvpPrefix, Globals.MvpNamespace);
            xpath.SetContext(mgr);

            int count1 = Document.CreateNavigator().Select(xpath).Count;
            // Test with compiled expression.
            int count2 = XPathCache.Select(expr, Document.CreateNavigator(), mgr).Count;

            Assert.AreEqual(count1, count2);

            // Test with dynamic expression.
            count2 = XPathCache.Select(dyn, Document.CreateNavigator(),
                                       mgr, new XPathVariable("price", price)).Count;

            Assert.AreEqual(count1, count2);
        }
Пример #2
0
        public void Reading()
        {
            Stream        stm = Globals.GetResource(Globals.PubsNsResource);
            XmlTextReader tr  = new XmlTextReader(stm);

            XPathDocument  doc = new XPathDocument(tr);
            XPathNavigator nav = doc.CreateNavigator();

            // Dumps the entire document to output.
            XmlTextWriter tw = new XmlTextWriter(Console.Out);

            tw.WriteNode(new XPathNavigatorReader(nav), true);

            // Perform a query, then dump the first node (OuterXml) from the result.
            nav = doc.CreateNavigator();
            XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);

            mgr.AddNamespace("mvp", "mvp-xml");
            XPathNodeIterator it = XPathCache.Select("//mvp:titles[mvp:title_id='BU2075']", nav, mgr);

            if (it.MoveNext())
            {
                XmlReader title = new XPathNavigatorReader(it.Current);
                // As it's a regular reader, we must first move it to content as usual.
                title.MoveToContent();
                Console.WriteLine(title.ReadOuterXml());
            }

            // Perform arbitrary movements, then serialize inner content.
            nav = doc.CreateNavigator();
            nav.MoveToFirstChild();
            nav.MoveToFirstChild();
            Console.WriteLine(new XPathNavigatorReader(nav).ReadInnerXml());
        }
        /// <summary>
        /// This code is just to show how it's used.
        /// If run it will throw exceptions.
        /// </summary>
        public void Test()
        {
            XPathNavigator    document = new XPathDocument(String.Empty).CreateNavigator();
            XPathNodeIterator it       = XPathCache.Select("/customer/order/item", document);

            it = XPathCache.Select("/customer/order[id=$id]/item", document,
                                   new XPathVariable("id", "23"));

            string[] ids = null;
            foreach (string id in ids)
            {
                it = XPathCache.Select("/customer/order[id=$id]/item", document,
                                       new XPathVariable("id", id));
            }

            XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);

            mgr.AddNamespace("po", "example-po");

            it = XPathCache.Select("/po:customer[id=$id]", document, mgr,
                                   new XPathVariable("id", "0736"));

            XmlDocument doc  = new XmlDocument();
            XmlNodeList list = XPathCache.SelectNodes("/customer", doc);
        }
        /// <summary>
        /// Evaluates <see cref="XPointer"/> pointer and returns 
        /// iterator over pointed nodes.
        /// </summary>
        /// <remarks>Note, that returned XPathNodeIterator is already moved once.</remarks>
        /// <param name="nav">XPathNavigator to evaluate the 
        /// <see cref="XPointer"/> on.</param>
        /// <returns><see cref="XPathNodeIterator"/> over pointed nodes</returns>	    					
        public override XPathNodeIterator Evaluate(XPathNavigator nav)
        {
            XPathNodeIterator result = XPathCache.Select("id('" + ncName + "')", nav, (XmlNamespaceManager)null);
            if (result != null && result.MoveNext())
            {
                return result;
            }

            throw new NoSubresourcesIdentifiedException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.NoSubresourcesIdentifiedException, ncName));
        }
Пример #5
0
 public override XPathNodeIterator Evaluate(XPathNavigator doc, XmlNamespaceManager nm)
 {
     try
     {
         return(XPathCache.Select(_xpath, doc, nm));
     }
     catch
     {
         return(null);
     }
 }
Пример #6
0
        /// <summary>
        /// Evaluates <see cref="XPointer"/> pointer and returns
        /// iterator over pointed nodes.
        /// </summary>
        /// <remarks>Note, that returned XPathNodeIterator is already moved once.</remarks>
        /// <param name="nav">XPathNavigator to evaluate the
        /// <see cref="XPointer"/> on.</param>
        /// <returns><see cref="XPathNodeIterator"/> over pointed nodes</returns>
        public override XPathNodeIterator Evaluate(XPathNavigator nav)
        {
            XPathNodeIterator result = XPathCache.Select("id('" + _NCName + "')", nav, (XmlNamespaceManager)null);

            if (result != null && result.MoveNext())
            {
                return(result);
            }
            else
            {
                throw new NoSubresourcesIdentifiedException(SR.GetString("NoSubresourcesIdentifiedException", _NCName));
            }
        }
Пример #7
0
        public void PrefixMapping()
        {
            string expr = "//mvp:titles[mvp:price > 10]";

            XPathNavigator docnav = Document.CreateNavigator();
            XPathExpression xpath = docnav.Compile(expr);
            XmlNamespaceManager mgr = new XmlNamespaceManager(docnav.NameTable);
            mgr.AddNamespace(Globals.MvpPrefix, Globals.MvpNamespace);
            xpath.SetContext(mgr);

            int count1 = Document.CreateNavigator().Select(xpath).Count;
            int count2 = XPathCache.Select(expr, Document.CreateNavigator(),
                new XmlPrefix(Globals.MvpPrefix, Globals.MvpNamespace, Document.CreateNavigator().NameTable)).Count;

            Assert.AreEqual(count1, count2);
        }
 public override XPathNodeIterator Evaluate(XPathNavigator doc,
                                            XmlNamespaceManager nm)
 {
     return(XPathCache.Select(XPath, doc, nm));
 }