.NET XPath starting from a specific element and backreferencing to it -


I have (in fact) an app where I want to use a specific xml element (called element A) Need XPath and then find another element (Ment B) referenced by Element A. Example:

  public static zero main (string [] arg) {var s = @ "" & lt; Root & gt; & Lt; Start RefId = "" 5 "" /> gt; & Lt; Start RefId = "" 6 "" /> > gt; & Lt; Item id = "6" "/> gt; & quot; root & gt;"; Var doc = new XmlDocument (); (Var sr = new string reader (s)) using {doc.Load (sr); } XMLNode Node = Doctor. Selection node (@ "root / start [1]") [0]; Var selected = node Selection node (@ "/ root / item [@id = @refId]"); }   

Here is the node node that I can find. There is a collection of selected node B which I can not find. My problem is in [@id = @refId], where I would like to return to the node, from which I start and get the value of its properties "RFID", but I have not figured out how. Is this possible?

Edit: The only way I can see in the comments below is to describe you Use XML, XPath variables from LINQ to complete, or define your own current () function. XPath variables and current functions are a bit of a pain because they require writing their classes to handle them, but you can use these relatively simple general classes:

  public classes Variable Contex: XsltContext {IXPathNavigable CurrentNode; Dictionary & lt; String, Object & gt; Value = New Dictionary & lt; String, Object & gt; (); Public Zero SetValue (string name, object value) {value [name] = value; } Public Zero SetCurrentNode (IXPathNavigable currentNode) {CurrentNode = currentNode; } Public override intrast document (string baseURI, string begone ury) {new non emulated exposition (throw); } Public override bool PreserveWhitespace (System.Xml.XPath.XPathNavigator node) {return false; } Public Override IXsltContextFunction ResolveFunction (String Prefix, String Name, System.Xml.XPath.XPathResultType [ArgTypes]) {Return (New. Current (CurrentNode)) (if name.Equals ("Current")); } Throwing new notified exception (); } Public override IXsltContextVariable ResolveVariable (string prefix, string name) {Object value; If (value TREGetValue (name, out value)) {new reference variable (name, value); } New ApplicationException Throw ("unknown variable:" + name); } Public Override Bull Voicespace {Return; }}} Public class reference variable: IXsltContextVariable {Private string m_name; Private object m_parameter; Public context (string name, object parameter) {m_name = name; M_parameter = parameter; IXPathNavigable navigable = IXPathNavigable as m_parameter; If (navigator! = Null) {m_parameter = navigable.CreateNavigator (). Choose ("."); }} # Field IXsltContextVariable member public object evaluation (XsltContext xsltContext) {return m_parameter; } Receive the public bool {OLEL} {returns true; }} Public bool IsParam {get true {return; }} Public XPathResultType VariableType {get {return XPathResultType.Any; }} # Ndrian} Public Squad Current Function: IXsltContextFunction {Private XPathNodeIterator CurrentNodeIterator; Internal current function (IXPathNavigable currentNode) {if (currentNode! = Null) {CurrentNodeIterator = currentNode.CreateNavigator () Choose ("."); }} Public XPathResultType [ArgTypes {get new XPathResultType {0]; }} Call the public object (XsltContext xsltContext, object [] args, XPathNavigator docContext) {if (CurrentNodeIterator == empty) {New Application Exception ("Current node is not set."); } Return CurrentNodeIterator; } Public Ent Maxages {Return {Return 0; }} Public Ent Mingers {Receipt {return 0; }} Public XPathResultType ReturnType {get {return XPathResultType.NodeSet; }}}   

Once you have been defined anywhere, you can:

  VariableContext vc = New VariableContext (); XMLNode Node = Doctor. Selectiondreaming node ("root / start [1]"); Vc.SetValue ("a", nodea); XmlNodeList selected = doc.SelectNodes ("/ root / item [@id = $ a / @ refId]", VC); Use   

or custom-defined function () function:

  VariableContext vc2 = new VariableContext (); XMLNode 2 = Doctor. Selected signals ("root / start [1]"); Vc2.SetCurrentNode (nodea2); XmlNodeList selected 2 = doc.SelectNodes ("/ root / item [@id = current () / @ refId]", vc2); PrintNodeList (selected2);   

Functional form:

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -