“ :”字符,十六进制值0x3A,不能包含在名称中

我有一个 xml 文件,其中包含如下元素

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

当我试图使用代码访问它时:

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

它给了我这个错误:

系统。韦伯。服务。协议。服务器无法处理请求。—— > 制度。Xml.XmlException: 名称中不能包含十六进制值0x3A 的“ :”字符。

我该怎么进去?

90185 次浏览

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.

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

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

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

Try to get namespace from the document

var ns = doc.Root.Name.Namespace;

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.

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