<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Element xsi:Attribute="Test"></Element>
</Root>
我正在尝试读取“ xsi:Attribute”属性;代码是这样的:
var doc = XDocument.Load(new StringReader(xmlText));
var node = doc.Root.Descendants().First();
XNamespace myNamespace = "xsi";
var attribute = node.Attributes(myNamespace + "Attribute").First();
它在最后一行抛出“序列不包含任何元素”异常。我究竟做错了什么?
您需要使用实际的名称空间,而不是"xsi"
,这只是XML文件本身中对实际名称空间的本地查找:
XNamespace myNamespace = "http://www.w3.org/2001/XMLSchema-instance";
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句