1

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?

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.....

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Teena Roy
  • 19
  • 5

1 Answers1

7

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:

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():

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().

dbc
  • 104,963
  • 20
  • 228
  • 340
  • get value without pass hard coded value....only pass attribute... hard coded value => ".htttp://wwvw.w3.org/1999/xlink." – Teena Roy Jun 09 '16 at 06:10
  • @TeenaRoy - If I understand your comment, `"http://www.w3.org/1999/xlink"` is not a hardcoded value, it's part of the full name of the attribute, in the same way that the full name of `XElement` is actually `System.Xml.Linq.XElement`. See [Understanding XML Namespaces](https://msdn.microsoft.com/en-us/library/aa468565.aspx). Rather, using the prefix `xlink:` would be hardcoding a value, since the prefix could be changed to anything else and the XML would be semantically identical, as long as the actual namespace is unchanged. – dbc Jun 09 '16 at 06:18
  • Hi, We have many similer cases like "xlink:href", so we would want it to be more dynamic. We tried with foreach loop for the node, but "xlink:href" is not coming when we get the name for the attribute. Is there a way where we can implement loop & can get the complete/full attribute name as "xlink:href" & then we can have the dynamic implementation in our app. – Teena Roy Jun 09 '16 at 07:58
  • @TeenaRoy - you can loop through all the attributes of an element with [`XElement.Attributes()`](https://msdn.microsoft.com/en-us/library/bb292315.aspx), then check the [`Name`](https://msdn.microsoft.com/en-us/library/system.xml.linq.xattribute.name.aspx) of each. However, `"xlink:href"` is NOT the name of the attribute. `xlink` is just a lookup key in the table of namespaces. The real name is `{http://www.w3.org/1999/xlink}href`. I don't believe the look-up prefix is captured directly in the `XAttribute`, you might need to crawl up the element hierarchy to find an appropriate prefix. – dbc Jun 09 '16 at 08:05
  • @TeenaRoy - It looks like you can use [`XElement.GetPrefixOfNamespace(XNamespace)`](https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.getprefixofnamespace.aspx) to get the prefix of a namespace. However, if you hardcode your parser to a particular lookup *prefix*, your parser is essentially broken, since it will fail to parse identical XML with different prefixes such as `http://www.ncbi.nlm.nih.gov/books/NBK154461/` – dbc Jun 09 '16 at 08:13
  • hi can you please tell me how to get "xlink:href" through xpath elements my code : var varSearchTerm = from x in xDocFile.XPathSelectElements("//ext-link[@xlink:href]") select x; – Teena Roy Jul 25 '16 at 05:03
  • @TeenaRoy - you should ask another question. The preferred format for questions on stackoverflow is [one question per post](https://meta.stackexchange.com/questions/222735). But maybe see [XPathSelectElement always returns null](https://stackoverflow.com/questions/5819305/xpathselectelement-always-returns-null) or [Weirdness with XDocument, XPath and namespaces](https://stackoverflow.com/questions/3715936). – dbc Jul 25 '16 at 11:21