gpt4 book ai didi

c# - Xdocument - 读取带有前缀的属性值

转载 作者:行者123 更新时间:2023-11-30 14:10:07 28 4
gpt4 key购买 nike

很容易找到 Xdocument 属性的值,但是我们如何找到带有前缀/命名空间的属性。

XML 代码

 <label:label xlink:type="resource" xlink:label="something" xlink:lang="en" xlink:id="res_4">My value</label:label>

我正在尝试读取属性值 xlink:Id(其中 p 是 XElement)

p => p.Attribute("xlink:id").Value 

这根本不起作用。

最佳答案

假设您在 XML 中的某处声明了命名空间前缀:

xmlns:xlink="dummy.url"

您可以使用指向上述 namespace URI 的 XNamespace 变量来访问 namespace 中的属性:

XNamespace xlink = "dummy.url";
.....
p => p.Attribute(xlink+"id").Value
//or simply cast the XAttribute to string
//to avoid exception when the attribute not found in p
p => (string)p.Attribute(xlink+"id")

关于c# - Xdocument - 读取带有前缀的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565961/

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