我有xml文件:
<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>
如何仅选择 Element("userid").value=="U01" 和 Element("name").Value=="Tom Jones" 等的那些(以及所有子节点/元素),即我想要结果:
<user_tuple>
<userid>U01</userid>
<code>
<name>Tom Jones</name>
</code>
<rating>B</rating>
</user_tuple>
我正在使用 C#
public void searchInfo(string rootNode, string Element1Name, string Element2Name, string Element1Val, string Element2Val){
////// Select rootNode and all 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)
}
例子:
searchInfo("rootNode","userid","code", "U01", "Tom Jones")
res = <userid>U01</userid>
<code>
<name>Tom Jones</name>
</code>
<rating>B</rating>
and result:
userid U01
name Tom Jones
rating B
那可能吗?