Take the 2-minute tour ×

I have an xml file that contains its element like


When I am trying to access it using the code:

XElement tempElement = doc.Descendants(XName.Get("ab:test")).FirstOrDefault();

It's giving me this error:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name.

How should I access it?

share|improve this question
You certainly have a way to handle namespaces so you don't have to (or in this case, you can't) put them in the name. You should look into this direction. –  p4bl0 Apr 4 '10 at 19:09
Not only does the XML specification say that ":" is allowed for names (and to start names!), but the Get method of XName doesn't document that it throws XmlException! –  Jim Jan 17 '13 at 18:19
add comment

5 Answers

up vote 45 down vote accepted

If you want to use namespaces, LINQ to XML makes that really easy:

XNamespace ab = "http://whatever-the-url-is";
XElement tempElement = doc.Descendants(ab + "test").FirstOrDefault();

Look for an xmlns:ab=... section in your document to find out which namespace URI "ab" refers to.

share|improve this answer
It works, but the problem is that value of xmlns:ab is generated dynamically based on time stamp. How can i get its value? –  coure2011 Apr 4 '10 at 20:31
@coure06: The namespace URI is dynamic? That's pretty weird. But yes, you can get it by finding the attribute value for XNamespace.Xmlns + "ab" from whichever element declares it. –  Jon Skeet Apr 4 '10 at 22:43
add comment

Try putting your namespace in { ... } like so:

string xfaNamespace = "{http://www.xfa.org/schema/xfa-template/2.6/}";
share|improve this answer
why should you use curly braces? What is the benefit? –  barrypicker Feb 11 at 20:47
That makes it work :) –  Serj Sagan Feb 12 at 17:41
add comment

There is an overload of the Get method you might want to try that takes into account the namespace. Try this:

XElement tempElement = doc.Descendants(XName.Get("test", "ab")).FirstOrDefault();
share|improve this answer
ab isn't the actual namespace here though - it's just the alias for the namespace. (I don't know the right terminology unfortunately.) LINQ to XML makes this easy with XNamespace. It's rare that you need to explicitly call XName.Get in LINQ to XML. –  Jon Skeet Apr 4 '10 at 19:15
ah my mistake, thanks for the clarification –  Blair Scott Apr 4 '10 at 22:37
add comment

Try to get namespace from the document

var ns = doc.Root.Name.Namespace;
share|improve this answer
add comment

I was having the same error. I found I was adding code...

var ab = "http://whatever-the-url-is";

... but ab was determined to be a string. This caused the error reported by OP. Instead of using the VAR keyword, I used the actual data type XNamespace...


... and the problem went away.

share|improve this answer
add comment

Your Answer


