85

I have an xml file that contains its element like

<ab:test>Str</ab:test>  

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?

yoozer8
  • 7,194
  • 6
  • 53
  • 88
coure2011
  • 37,153
  • 75
  • 206
  • 328
  • 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 04 '10 at 19:09
  • 2
    Not only does the XML specification say that ":" is [allowed for names](http://www.w3.org/TR/REC-xml/#NT-NameStartChar) (and to start names!), but the [Get method](http://msdn.microsoft.com/en-us/library/bb357369.aspx) of XName doesn't document that it throws XmlException! – yoozer8 Jan 17 '13 at 18:19
  • See also https://stackoverflow.com/questions/8324960/creating-xdocument-with-xsischemalocation-namespace for correct namespace handling. – Polluks Sep 13 '18 at 09:45

6 Answers6

125

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.

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • 2
    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 04 '10 at 20:31
  • 6
    @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 04 '10 at 22:43
  • I am getting this error: http://stackoverflow.com/questions/42839628/how-to-get-certain-elements-inside-an-xml-document/42840373#42840373 – Si8 Mar 16 '17 at 17:15
  • Interestingly this doesn't seem to work with the newer string templating syntax, so `$"{ab}test"` gives the same error – Liam May 20 '19 at 10:54
  • 4
    @Liam: I'd expect that - `ab + "test"` isn't performing string concatenation; it's using the `+(XNamespace, string)` operator to create an `XName`. – Jon Skeet May 20 '19 at 10:55
27

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

string xfaNamespace = "{http://www.xfa.org/schema/xfa-template/2.6/}";
Serj Sagan
  • 26,825
  • 17
  • 143
  • 176
  • 1
    why should you use curly braces? What is the benefit? – barrypicker Feb 11 '14 at 20:47
  • 1
    Using the `XNamespace` method essentially just does this for you. `.ToString` on a `XNamespace` obviously just formats the string correctly for you. So this is the same tnhing done a different way, there is no benefit in either – Liam May 20 '19 at 09:52
19

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

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

... and the problem went away.

barrypicker
  • 9,050
  • 8
  • 59
  • 75
7

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();
Blair Scott
  • 1,875
  • 2
  • 19
  • 29
  • 1
    `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 04 '10 at 19:15
5

Try to get namespace from the document

var ns = doc.Root.Name.Namespace;
Jason Dias
  • 759
  • 1
  • 7
  • 9
0

Deleting AndroidManifest.xml and AndroidManifest.xml.DISABLED worked for me.

Grigory Zhadko
  • 1,234
  • 1
  • 16
  • 29