c# - ':'字符,十六进制值0x3A,不能包含在名称中

标签 c# xml linq linq-to-xml

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

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

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

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

它给我这个错误:

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.

我应该如何访问它?

最佳答案

如果您想使用命名空间,LINQ to XML 可以让这变得非常简单:

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

在文档中查找 xmlns:ab=... 部分,找出“ab”所指的命名空间 URI。

关于c# - ':'字符,十六进制值0x3A,不能包含在名称中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575546/

相关文章:

c# - ASP.net MVC OutputCache 参数优先级 - Duration 与 SQLDependency

html - Android中解析HTML : no XSLT? 有什么用?

c# - 有没有更好的方法用另一个列表搜索列表

c# - 使用 linq 生成动态集合

C# Interop - 无法创建对象

c# - 类型或命名空间定义错误

java - 由于 null 无法执行 getNextSibling()

c# - FPML 计算

c# - 使用 linq 按具有空数据值的日期进行分组

c# - WPF Listview中的多选和多种样式