linq xml select node -
i have xml document:
<users> <user_tuple> <userid>u01</userid> <code> <name>tom jones</name> </code> <rating>b</rating> </user_tuple> <user_tuple> <userid>u02</userid> <code> <name>mary doe</name> </code> <rating>a</rating> </user_tuple> <user_tuple> <userid>u03</userid> <code> <name>dee linquent</name> </code> <rating>d</rating> </user_tuple>
how select (and child nodes/elements) element("userid").value=="u01" , element("name").value=="tom jones" etc ie want results:
<user_tuple> <userid>u01</userid> <code> <name>tom jones</name> </code> <rating>b</rating> </user_tuple>
i'm using c#
public void searchinfo(string rootnode, string element1name, string element2name, string element1val, string element2val){ ////// select rootnode , descend nodes var res = root.elements("rootnode") .where( x => (string)x.element(element1name) == element1val&& (string)x.element(element2name) == element2val) ).tolist(); /////////// foreach (var node in res){ debug.writeline("name {0} value {1}", node.name, node.value) }
example:
searchinfo("rootnode","userid","code", "u01", "tom jones")
res = <userid>u01</userid> <code> <name>tom jones</name> </code> <rating>b</rating> , result: userid u01 name tom jones rating b
is possible?
try this
var doc = xdocument.parse(s); var res = doc.elements("users").elements("user_tuple") .where( x => (string)x.element("userid") == "u01" && (string)x.element("code").element("name") == "tomjones") ).tolist();
or use xpath:
var res = doc.xpathselectelements(" users/user_tuple[userid='u01' , code[name='tomjones']] ").tolist();
Comments
Post a Comment