The ':' character, hexadecimal value 0x3A, cannot be included in a name

C#XmlLinqLinq to-Xml

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

string xfaNamespace = "{http://www.xfa.org/schema/xfa-template/2.6/}";

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

Try to get namespace from the document

var ns = doc.Root.Name.Namespace;

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncoure2011View Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Serj SaganView Answer on Stackoverflow
Solution 3 - C#barrypickerView Answer on Stackoverflow
Solution 4 - C#Blair ScottView Answer on Stackoverflow
Solution 5 - C#Jason DiasView Answer on Stackoverflow
Solution 6 - C#Abdelfettah LemkhanterView Answer on Stackoverflow