Reputation: 42444
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?
Upvotes: 95
Views: 104326
Reputation: 56
The ':' character is problematic when it is contained in namespace. Example :
<?xml version="1.0"?>
<SAMLConfiguration xmlns="urn:componentspace:SAML:2.0:configuration">
<ServiceProvider Name="http://avanteam"
Description="Avanteam Service Provider"
AssertionConsumerServiceUrl="SAML/AssertionConsumerService"
LocalCertificateFile="Certificates\sp.pfx"
LocalCertificatePassword="password"/>
</SAMLConfiguration>
A solution that works in all cases is to use the GetName
method on an instance of XNamespace
. Example with default namespace:
var ns = doc.Root.GetDefaultNamespace();
var serviceProviderNode = doc.Element(ns.GetName("SAMLConfiguration"))?.Element(ns.GetName("ServiceProvider"));
Upvotes: 0
Reputation: 1
Deleting AndroidManifest.xml
and AndroidManifest.xml.DISABLED
worked for me.
Upvotes: 0
Reputation: 10088
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.
Upvotes: 22
Reputation: 771
Try to get namespace from the document
var ns = doc.Root.Name.Namespace;
Upvotes: 5
Reputation: 30208
Try putting your namespace in {
... }
like so:
string xfaNamespace = "{http://www.xfa.org/schema/xfa-template/2.6/}";
Upvotes: 29
Reputation: 1885
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();
Upvotes: 8
Reputation: 1500525
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.
Upvotes: 134