gpt4 book ai didi

c# - 获取特定的xelement子值

转载 作者:行者123 更新时间:2023-12-02 05:37:56 25 4
gpt4 key购买 nike

<information items ="2">
<table id="31"> </table>
<profile code="5">
<name language="ro"> Spania </name>
<name language="gb"> Spain </name>
<name language="pl"> Hiszpania </name>
</profile>
</information>

我确实想获取元素 <name> 的值具有其属性 language = "gb"

我尝试过类似的方法:

string country = xdoc.Descendants("information").Elements("profile").Elements("name")./*???Value???*?

我怎样才能实现这个目标?

最佳答案

您可以使用XPath :

System.Xml.Linq.XDocument :

string country = xdoc.XPathSelectElement("/information/profile/name[@language='gb']").Value;

System.Xml.XmlDocument :

string country = xdoc.SelectSingleNode("/information/profile/name[@language='gb']").InnerText;

请记住,您还需要 System.Xml.XPath 命名空间。

关于c# - 获取特定的xelement子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com