Beispiel #1
0
                /// <summary>
                /// Tests the ContentBeforeSelf method on Node.
                /// </summary>
                /// <param name="context"></param>
                /// <returns></returns>
                //[Variation(Desc = "NodeContentBeforeSelf")]
                public void NodeContentBeforeSelf()
                {
                    XElement parent = new XElement("parent");

                    XComment child = new XComment("Self is a comment");

                    XComment   comment1  = new XComment("Another comment");
                    XComment   comment2  = new XComment("Yet another comment");
                    XElement   element1  = new XElement("childelement", new XElement("nested"), new XAttribute("foo", "bar"));
                    XElement   element2  = new XElement("childelement2", new XElement("nested"), new XAttribute("foo", "bar"));
                    XAttribute attribute = new XAttribute("attribute", "value");

                    // If no parent, should not be any content before it.
                    Validate.Enumerator(child.NodesBeforeSelf(), new XNode[0]);

                    // Add some content, including the child, and validate.
                    parent.Add(attribute);
                    parent.Add(comment1);
                    parent.Add(element1);

                    parent.Add(child);

                    parent.Add(comment2);
                    parent.Add(element2);

                    Validate.Enumerator(
                        child.NodesBeforeSelf(),
                        new XNode[] { comment1, element1 });
                }
Beispiel #2
0
                /// <summary>
                /// Tests the AllContentBeforeSelf method on Node.
                /// </summary>
                /// <param name="context"></param>
                /// <returns></returns>
                //[Variation(Desc = "NodeAllContentBeforeSelf")]
                public void NodeAllContentBeforeSelf()
                {
                    XElement parent = new XElement("parent");

                    XComment child = new XComment("Self is a comment");

                    XComment   comment1  = new XComment("Another comment");
                    XComment   comment2  = new XComment("Yet another comment");
                    XElement   element1  = new XElement("childelement", new XElement("nested"), new XAttribute("foo", "bar"));
                    XElement   element2  = new XElement("childelement2", new XElement("nested"), new XAttribute("foo", "bar"));
                    XAttribute attribute = new XAttribute("attribute", "value");

                    // If no parent, should not be any content before it.
                    Validate.Enumerator <XNode>(child.NodesBeforeSelf(), new XNode[0]);

                    // Add child to parent. Should still be no content before it.
                    // Attributes are not content.
                    parent.Add(attribute);
                    parent.Add(child);
                    Validate.Enumerator <XNode>(child.NodesBeforeSelf(), new XNode[0]);

                    // Add more children and validate.
                    parent.Add(comment1);
                    parent.Add(element1);

                    Validate.Enumerator <XNode>(child.NodesBeforeSelf(), new XNode[0]);

                    parent.AddFirst(element2);
                    parent.AddFirst(comment2);

                    Validate.Enumerator <XNode>(child.NodesBeforeSelf(), new XNode[] { comment2, element2 });
                }
Beispiel #3
0
 public static string GetXPath(this XObject xobj)
 {
     if (xobj.Parent == null)
     {
         XDocument doc = xobj as XDocument;
         if (doc != null)
         {
             return(".");
         }
         XElement el = xobj as XElement;
         if (el != null)
         {
             return("/" + NameWithPredicate(el));
         }
         XText xt = xobj as XText;
         if (xt != null)
         {
             return(null);
         }
         //
         //the following doesn't work because the XPath data
         //model doesn't include white space text nodes that
         //are children of the document.
         //
         //return
         //    "/" +
         //    (
         //        xt
         //        .Document
         //        .Nodes()
         //        .OfType<XText>()
         //        .Count() != 1 ?
         //        "text()[" +
         //        (xt
         //        .NodesBeforeSelf()
         //        .OfType<XText>()
         //        .Count() + 1) + "]" :
         //        "text()"
         //    );
         //
         XComment com = xobj as XComment;
         if (com != null)
         {
             return
                 ("/" +
                  (
                      com
                      .Document
                      .Nodes()
                      .OfType <XComment>()
                      .Count() != 1 ?
                      "comment()[" +
                      (com
                       .NodesBeforeSelf()
                       .OfType <XComment>()
                       .Count() + 1) +
                      "]" :
                      "comment()"
                  ));
         }
         XProcessingInstruction pi = xobj as XProcessingInstruction;
         if (pi != null)
         {
             return
                 ("/" +
                  (
                      pi.Document.Nodes()
                      .OfType <XProcessingInstruction>()
                      .Count() != 1 ?
                      "processing-instruction()[" +
                      (pi
                       .NodesBeforeSelf()
                       .OfType <XProcessingInstruction>()
                       .Count() + 1) +
                      "]" :
                      "processing-instruction()"
                  ));
         }
         return(null);
     }
     else
     {
         XElement el = xobj as XElement;
         if (el != null)
         {
             return
                 ("/" +
                  el
                  .Ancestors()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  NameWithPredicate(el));
         }
         XAttribute at = xobj as XAttribute;
         if (at != null)
         {
             return
                 ("/" +
                  at
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  "@" + GetQName(at));
         }
         XComment com = xobj as XComment;
         if (com != null)
         {
             return
                 ("/" +
                  com
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      com
                      .Parent
                      .Nodes()
                      .OfType <XComment>()
                      .Count() != 1 ?
                      "comment()[" +
                      (com
                       .NodesBeforeSelf()
                       .OfType <XComment>()
                       .Count() + 1) + "]" :
                      "comment()"
                  ));
         }
         XCData cd = xobj as XCData;
         if (cd != null)
         {
             return
                 ("/" +
                  cd
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      cd
                      .Parent
                      .Nodes()
                      .OfType <XText>()
                      .Count() != 1 ?
                      "text()[" +
                      (cd
                       .NodesBeforeSelf()
                       .OfType <XText>()
                       .Count() + 1) + "]" :
                      "text()"
                  ));
         }
         XText tx = xobj as XText;
         if (tx != null)
         {
             return
                 ("/" +
                  tx
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      tx
                      .Parent
                      .Nodes()
                      .OfType <XText>()
                      .Count() != 1 ?
                      "text()[" +
                      (tx
                       .NodesBeforeSelf()
                       .OfType <XText>()
                       .Count() + 1) + "]" :
                      "text()"
                  ));
         }
         XProcessingInstruction pi = xobj as XProcessingInstruction;
         if (pi != null)
         {
             return
                 ("/" +
                  pi
                  .Parent
                  .AncestorsAndSelf()
                  .InDocumentOrder()
                  .Select(e => NameWithPredicate(e))
                  .StrCat("/") +
                  (
                      pi
                      .Parent
                      .Nodes()
                      .OfType <XProcessingInstruction>()
                      .Count() != 1 ?
                      "processing-instruction()[" +
                      (pi
                       .NodesBeforeSelf()
                       .OfType <XProcessingInstruction>()
                       .Count() + 1) + "]" :
                      "processing-instruction()"
                  ));
         }
         return(null);
     }
 }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xobj"></param>
        /// <returns></returns>
        public static string GetXPath(this XObject xobj)
        {
            if (xobj.Parent == null)
            {
                XDocument doc = xobj as XDocument;
                if (doc != null)
                {
                    return(".");
                }
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return("/" + NameWithPredicate(el));
                }
                // the XPath data model does not include white space text nodes
                // that are children of a document, so this method returns null.
                XText xt = xobj as XText;
                if (xt != null)
                {
                    return(null);
                }
                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         (
                             com
                             .Document
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) +
                             "]" :
                             "comment()"
                         ));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         (
                             pi.Document.Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) +
                             "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
            else
            {
                XElement el = xobj as XElement;
                if (el != null &&
                    !el.HasElements) // Only when the element is a Leaf element
                {
                    return
                        ("/" +
                         el
                         .Ancestors()
                         .InDocumentOrder()
                         .Select(e => NameWithPredicate(e))
                         .StrCat("/") +
                         NameWithPredicate(el));
                }
                XAttribute at = xobj as XAttribute;
                if (at != null)
                {
                    return
                        ("/" +
                         at
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(e => NameWithPredicate(e))
                         .StrCat("/") +
                         "@" + GetQName(at));
                }
                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         com
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(e => NameWithPredicate(e))
                         .StrCat("/") +
                         (
                             com
                             .Parent
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) + "]" :
                             "comment()"
                         ));
                }
                XCData cd = xobj as XCData;
                if (cd != null)
                {
                    return
                        ("/" +
                         cd
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(e => NameWithPredicate(e))
                         .StrCat("/") +
                         (
                             cd
                             .Parent
                             .Nodes()
                             .OfType <XText>()
                             .Count() != 1 ?
                             "text()[" +
                             (cd
                              .NodesBeforeSelf()
                              .OfType <XText>()
                              .Count() + 1) + "]" :
                             "text()"
                         ));
                }

                /*
                 * XText tx = xobj as XText;
                 * if (tx != null)
                 *  return
                 *      "/" +
                 *      tx
                 *      .Parent
                 *      .AncestorsAndSelf()
                 *      .InDocumentOrder()
                 *      .Select(e => NameWithPredicate(e))
                 *      .StrCat("/") +
                 *      (
                 *          tx
                 *          .Parent
                 *          .Nodes()
                 *          .OfType<XText>()
                 *          .Count() != 1 ?
                 *          "text()[" +
                 *          (tx
                 *          .NodesBeforeSelf()
                 *          .OfType<XText>()
                 *          .Count() + 1) + "]" :
                 *          "text()"
                 *      );
                 */
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         pi
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(e => NameWithPredicate(e))
                         .StrCat("/") +
                         (
                             pi
                             .Parent
                             .Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) + "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
        }
        /// <summary>
        /// Gets the X path.
        /// </summary>
        /// <param name="xobj">The xobj.</param>
        /// <returns>The x Path.</returns>
        public static string GetXPath(this XObject xobj)
        {
            if (xobj.Parent == null)
            {
                XDocument doc = xobj as XDocument;
                if (doc != null)
                {
                    return(".");
                }
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return("/" + NameWithPredicate(el));
                }

                // the XPath data model does not include white space text nodes
                // that are children of a document, so this method returns null.
                XText xt = xobj as XText;
                if (xt != null)
                {
                    return(null);
                }
                XComment com = xobj as XComment;
                if (com?.Document != null)
                {
                    return("/" + (com.Document.Nodes().OfType <XComment>().Count() != 1 ? "comment()[" + (com.NodesBeforeSelf().OfType <XComment>().Count() + 1) + "]" : "comment()"));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi?.Document != null)
                {
                    return("/" + (pi.Document.Nodes().OfType <XProcessingInstruction>().Count() != 1 ? "processing-instruction()[" + (pi.NodesBeforeSelf().OfType <XProcessingInstruction>().Count() + 1) + "]" : "processing-instruction()"));
                }
                return(null);
            }
            else
            {
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return("/" + el.Ancestors().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + NameWithPredicate(el));
                }

                XAttribute at = xobj as XAttribute;
                if (at?.Parent != null)
                {
                    return("/" + at.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + "@" + GetQName(at));
                }
                XComment com = xobj as XComment;
                if (com?.Parent != null)
                {
                    return("/" + com.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (com.Parent.Nodes().OfType <XComment>().Count() != 1 ? "comment()[" + (com.NodesBeforeSelf().OfType <XComment>().Count() + 1) + "]" : "comment()"));
                }
                XCData cd = xobj as XCData;
                if (cd?.Parent != null)
                {
                    return("/" + cd.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (cd.Parent.Nodes().OfType <XText>().Count() != 1 ? "text()[" + (cd.NodesBeforeSelf().OfType <XText>().Count() + 1) + "]" : "text()"));
                }
                XText tx = xobj as XText;
                if (tx?.Parent != null)
                {
                    return("/" + tx.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (tx.Parent.Nodes().OfType <XText>().Count() != 1 ? "text()[" + (tx.NodesBeforeSelf().OfType <XText>().Count() + 1) + "]" : "text()"));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi?.Parent != null)
                {
                    return("/" + pi.Parent.AncestorsAndSelf().InDocumentOrder().Select(NameWithPredicate).StrCat("/") + (pi.Parent.Nodes().OfType <XProcessingInstruction>().Count() != 1 ? "processing-instruction()[" + (pi.NodesBeforeSelf().OfType <XProcessingInstruction>().Count() + 1) + "]" : "processing-instruction()"));
                }
                return(null);
            }
        }
        /// <summary>
        /// Recupera l'esspressione xpath di un determinato XObject
        /// </summary>
        /// <param name="xobj">Oggetto da valutare</param>
        /// <returns>xpath in formato stringa</returns>
        public static string GetXPath(this XObject xobj)
        {
            if (xobj.Parent == null)
            {
                var doc = xobj as XDocument;
                if (doc != null)
                {
                    return(".");
                }

                var el = xobj as XElement;
                if (el != null)
                {
                    return("/" + NameWithPredicate(el));
                }

                XText xt = xobj as XText;
                if (xt != null)
                {
                    return(null);
                }

                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         (
                             com
                             .Document
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) +
                             "]" :
                             "comment()"
                         ));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         (
                             pi.Document.Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) +
                             "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
            else
            {
                XElement el = xobj as XElement;
                if (el != null)
                {
                    return
                        ("/" +
                         el
                         .Ancestors()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         NameWithPredicate(el));
                }
                XAttribute at = xobj as XAttribute;
                if (at != null)
                {
                    return
                        ("/" +
                         at
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         "@" + GetQName(at));
                }
                XComment com = xobj as XComment;
                if (com != null)
                {
                    return
                        ("/" +
                         com
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             com
                             .Parent
                             .Nodes()
                             .OfType <XComment>()
                             .Count() != 1 ?
                             "comment()[" +
                             (com
                              .NodesBeforeSelf()
                              .OfType <XComment>()
                              .Count() + 1) + "]" :
                             "comment()"
                         ));
                }
                XCData cd = xobj as XCData;
                if (cd != null)
                {
                    return
                        ("/" +
                         cd
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             cd
                             .Parent
                             .Nodes()
                             .OfType <XText>()
                             .Count() != 1 ?
                             "text()[" +
                             (cd
                              .NodesBeforeSelf()
                              .OfType <XText>()
                              .Count() + 1) + "]" :
                             "text()"
                         ));
                }
                XText tx = xobj as XText;
                if (tx != null)
                {
                    return
                        ("/" +
                         tx
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             tx
                             .Parent
                             .Nodes()
                             .OfType <XText>()
                             .Count() != 1 ?
                             "text()[" +
                             (tx
                              .NodesBeforeSelf()
                              .OfType <XText>()
                              .Count() + 1) + "]" :
                             "text()"
                         ));
                }
                XProcessingInstruction pi = xobj as XProcessingInstruction;
                if (pi != null)
                {
                    return
                        ("/" +
                         pi
                         .Parent
                         .AncestorsAndSelf()
                         .InDocumentOrder()
                         .Select(NameWithPredicate)
                         .StrCat("/") +
                         (
                             pi
                             .Parent
                             .Nodes()
                             .OfType <XProcessingInstruction>()
                             .Count() != 1 ?
                             "processing-instruction()[" +
                             (pi
                              .NodesBeforeSelf()
                              .OfType <XProcessingInstruction>()
                              .Count() + 1) + "]" :
                             "processing-instruction()"
                         ));
                }
                return(null);
            }
        }