在XML文件中使用冒号时获取属性

时间:2021-10-24 03:40:12

I met a question that I can't get the attribute in the xml when the colon exists. For example, I want to get the value but it seems not working with my code. Any suggestions?

我遇到一个问题,当冒号存在时,我无法在xml中获取属性。例如,我想获得值,但它似乎不能使用我的代码。有什么建议吗?

For Example:

例如:

Xml content:
<ext-link xlink:href="http://www.ncbi.nlm.nih.gov/books/NBK154461/" ext-link-type="uri" xmlns:xlink="http://www.w3.org/1999/xlink">http://www.ncbi.nlm.nih.gov/books/NBK154461/</ext-link>


My code:
foreach(XElement xeTmp in Elementlist)
{
string strValue = xeTmp.Attribute("xlink:href").Value;
}

Exception:

例外:

 {"The ':' character, hexadecimal value 0x3A, cannot be included in a
 name."}

please suggest how to fixed it.....

请建议如何修复它……

1 个解决方案

#1


6  

First, the xlink: prefix is the namespace prefix for the attribute. A namespace prefix has no intrinsic meaning, it's just a lookup for the namespace corresponding to the prefix, which must be declared by an xmlns:xlink="..." attribute in scope, in this case:

首先,xlink:前缀是属性的名称空间前缀。名称空间前缀没有内在含义,它只是与前缀对应的名称空间的查找,在这种情况下,必须在作用域中使用xmlns:xlink="…"属性声明该名称空间。

xmlns:xlink="http://www.w3.org/1999/xlink"

Second, the XElement.Attribute(XName) method actually takes an XName argument. This class represents the combination of an XML namespace and name, thus allowing you to specify the attribute namespace and name to search for using XName.Get():

其次,XElement.Attribute(XName)方法实际上接受一个XName参数。这个类表示XML名称空间和名称的组合,因此允许您指定使用XName.Get()搜索的属性名称空间和名称:

var strValue = (string)xeTmp.Attribute(XName.Get("href", "http://www.w3.org/1999/xlink"));

Or equivalently using implicit operators:

或者等价地使用隐式运算符:

var strValue = (string)xeTmp.Attribute(((XNamespace)"http://www.w3.org/1999/xlink") + "href");

To loop through all attributes of an element, you can use XElement.Attributes().

要循环遍历元素的所有属性,可以使用XElement.Attributes()。

#1


6  

First, the xlink: prefix is the namespace prefix for the attribute. A namespace prefix has no intrinsic meaning, it's just a lookup for the namespace corresponding to the prefix, which must be declared by an xmlns:xlink="..." attribute in scope, in this case:

首先,xlink:前缀是属性的名称空间前缀。名称空间前缀没有内在含义,它只是与前缀对应的名称空间的查找,在这种情况下,必须在作用域中使用xmlns:xlink="…"属性声明该名称空间。

xmlns:xlink="http://www.w3.org/1999/xlink"

Second, the XElement.Attribute(XName) method actually takes an XName argument. This class represents the combination of an XML namespace and name, thus allowing you to specify the attribute namespace and name to search for using XName.Get():

其次,XElement.Attribute(XName)方法实际上接受一个XName参数。这个类表示XML名称空间和名称的组合,因此允许您指定使用XName.Get()搜索的属性名称空间和名称:

var strValue = (string)xeTmp.Attribute(XName.Get("href", "http://www.w3.org/1999/xlink"));

Or equivalently using implicit operators:

或者等价地使用隐式运算符:

var strValue = (string)xeTmp.Attribute(((XNamespace)"http://www.w3.org/1999/xlink") + "href");

To loop through all attributes of an element, you can use XElement.Attributes().

要循环遍历元素的所有属性,可以使用XElement.Attributes()。