gpt4 book ai didi

c# - 从 XNode 检索属性值

转载 作者:数据小太阳 更新时间:2023-10-29 02:26:12 26 4
gpt4 key购买 nike

我加载了我的 XML 文件,然后我选择了我的最后一个元素。这是代码:

  XDocument doc = XDocument.Load("something.xml");
var last = doc.Root.LastNode;

上面的代码输出 XML 文件中的最后一个元素。这是代码:

  <link num="4" url="yahoo.com">Yahoo</link>

我希望能够选择 num 的值 4。这是代码:

  num="4"

如何从我的最后一个节点中选择数字 4?

最佳答案

试试这个:

XDocument xDoc = XDocument.Parse(xml);
string num = xDoc.Root.Elements().Last().Attribute("num").Value;
Console.WriteLine(num);

确保您已添加以下内容:

using System.Linq;
using System.Xml.Linq;

关于c# - 从 XNode 检索属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227653/

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