我需要检查例如“Charrizard”是否存在,我在网上查找但只找到了 xElement 属性值和子节点示例。
<pokemons>
<pokemon>
<color>red</color>
<name>Charrizard</name> //the content is named value right ??
</pokemon>
</pokemons>
我在某个地方看到它的开头是这样的:
XDocument doc = XDocument.Load("pokemons.xml");
bool b = doc.Descendants(but don't know how to access the value.)..
最佳答案
bool b = doc.Descendants("name").Any(x=> x.Value == "Charrizard");
您可以使用 Enumerable.Any 来实现这一点
Determines whether any element of a sequence satisfies a condition.
这里是 dotNetFiddle 中的完整示例
关于c# - 如何检查xElement值是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146243/