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); } }
/// <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> /// /// </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> /// 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); } }