很容易找到 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/