/// <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);
        }
Example #2
0
 public static IEnumerable <XmlElement> ElementsByXPath(this XmlNode node, string xPath)
 {
     try
     {
         return(XPathCache.SelectNodes(xPath, node).OfType <XmlElement>());
     }
     catch (NullReferenceException)
     {
         return(node.SelectNodes(xPath).OfType <XmlElement>());
     }
 }
Example #3
0
        public string CallAction(string action, string input, IProgressCallback progressReporter)
        {
            XmlNode     fault;
            XmlDocument outputDoc = null;
            var         inputDoc  = new XmlDocument();

            inputDoc.LoadXml(input);
            if (_userInfo == null)
            {
                _userInfo = _inn.applyAML(string.Format("<AML><Item type='User' action='get' select='default_vault' expand='1'><id>{0}</id><Relationships><Item type='ReadPriority' action='get' select='priority, related_id' expand='1' orderBy='priority'/></Relationships></Item></AML>", _inn.getUserID()));
            }

            if (action == "ApplyItem" || action == "ApplyAML")
            {
                var     fileNodes = XPathCache.SelectNodes("descendant-or-self::Item[@type='File' and (@action='add' or @action='update' or @action='create') and actual_filename]", inputDoc.DocumentElement);
                XmlNode locatedNode;
                if (fileNodes.Count > 0)
                {
                    Item fileItem = _inn.newItem();
                    foreach (var fileNode in fileNodes.OfType <XmlElement>())
                    {
                        if (string.IsNullOrEmpty(fileNode.Attribute("id")))
                        {
                            fileNode.Attr("id", _inn.getNewID());
                        }
                        fileNode.Elem("checkedout_path", Path.GetDirectoryName(fileNode.Element("actual_filename", "")));
                        fileNode.Elem("filename", Path.GetFileName(fileNode.Element("actual_filename", "")));
                        locatedNode = XPathCache.SelectSingleNode("Relationships/Item[@type='Located']/related_id", fileNode);
                        if (locatedNode == null)
                        {
                            fileItem.dom      = inputDoc;
                            fileItem.node     = (XmlElement)fileNode;
                            fileItem.nodeList = null;
                            fileItem.attachPhysicalFile(fileNode.Element("actual_filename", ""), _userInfo.getProperty("default_vault"));
                        }
                    }

                    var firstItem = XPathCache.SelectSingleNode("//Item[1]", inputDoc.DocumentElement);
                    IList <XmlElement> items;
                    if (firstItem.ParentNode == null)
                    {
                        items = new XmlElement[] { (XmlElement)firstItem };
                    }
                    else
                    {
                        items = firstItem.Parent().Elements("Item").ToList();
                    }

                    Item       result;
                    XmlElement resultNode = null;

                    for (var i = 0; i < items.Count; i++)
                    {
                        fileItem.dom      = items[i].OwnerDocument;
                        fileItem.node     = items[i];
                        fileItem.nodeList = null;
                        result            = fileItem.apply();
                        fault             = XPathCache.SelectSingleNode(faultXPath, result.dom.DocumentElement);
                        if (fault != null)
                        {
                            fault.AppendChild(result.dom.CreateElement("original_query")).InnerText = input;
                            return(result.dom.DocumentElement.OuterXml);
                        }
                        else if (result.isError())
                        {
                            throw new InvalidOperationException();
                        }

                        if (outputDoc == null)
                        {
                            outputDoc  = result.dom;
                            resultNode = XPathCache.SelectSingleNode("//Item[1]", outputDoc.DocumentElement).Parent() as XmlElement;
                        }
                        else
                        {
                            resultNode.AppendChild(outputDoc.ImportNode(result.node, true));
                        }

                        if (progressReporter != null)
                        {
                            progressReporter.ReportProgress(i + 1, items.Count);
                        }
                    }

                    return(outputDoc.OuterXml);
                }
            }

            outputDoc = new XmlDocument();
            outputDoc.Elem("Empty");
            _inn.getConnection().CallAction(action, inputDoc, outputDoc);
            fault = XPathCache.SelectSingleNode(faultXPath, outputDoc.DocumentElement);
            if (fault != null)
            {
                fault.AppendChild(outputDoc.CreateElement("original_query")).InnerText = input;
            }
            return(outputDoc.DocumentElement.OuterXml);
        }
Example #4
0
 public static IEnumerable <XmlNode> XPath(this XmlNode node, string xPath, params object[] vars)
 {
     return(XPathCache.SelectNodes(xPath, node,
                                   vars.Select((v, i) => new XPathVariable("p" + i.ToString(), v)).ToArray())
            .OfType <XmlNode>());
 }
Example #5
0
 public static IEnumerable <XmlNode> XPath(this XmlNode node, string xPath)
 {
     return(XPathCache.SelectNodes(xPath, node).OfType <XmlNode>());
 }