complex Xpath into Linq to XML -
my input xml:
.... <node attribute1 = "guid1"> <childnode1 attributechildnode= var1> </childnode> <childnode2 attributechildnode= var2> </childnode> </node> <node attribute1 = "guid2"> <childnode3 attributechildnode= var3> </childnode> <childnode4 attributechildnode= var4> </childnode> </node> ....
my xpath code looks this
mynodelist = xmldoc.selectnodes(".//node[@attribute1 ='" & varstring1 &'']/nodechild[@attributechildnode1 = ''& varstring2 &'']")
i have no idea how linq xml code should same result can please me
you can still use xpath using xdocument
, linq xml;
var doc = xdocument.load(filepath); var mynodelist = doc.xpathselectelements(".//node[@attribute1 ='" & varstring1 &'']/nodechild[@attributechildnode1 = ''& varstring2 &'']");
xpathselectelements
declared extension method on xnode
, within system.xml.xpath
namespace.
standard linq xml version
input xml:
<root> <node attribute1="guid1"> <childnode attributechildnode1="var1" attributechildnode2="result1"></childnode> <childnode attributechildnode1="var2" attributechildnode2="result2"></childnode> </node> <node attribute1="guid2"> <childnode attributechildnode1="var3" attributechildnode2="result3"></childnode> <childnode attributechildnode1="var4" attributechildnode2="result4"></childnode> </node> </root>
query:
string guid = "guid1"; string var = "var1"; var elements = xelement.load("input.txt"); var value = (from node in elements.elements("node") (string)node.attribute("attribute1") == "guid1" childnode in node.elements() (string)childnode.attribute("attributechildnode1") == "var1" select (string)childnode.attribute("attributechildnode2")).firstordefault();
returns result1
- result4
, depending on guid
, var
variables values.
Comments
Post a Comment