加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

有没有办法在Linq-to-XML查询中仅使用本地名称检索元素?

发布时间:2020-12-16 07:48:28 所属栏目:百科 来源:网络整理
导读:让我们假设我们有这个xml: ?xml version="1.0" encoding="UTF-8"?tns:RegistryResponse status="urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure" xmlns:tns="urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0" xmlns:rim="urn:oasis:names:tc:ebx
让我们假设我们有这个xml:
<?xml version="1.0" encoding="UTF-8"?>
<tns:RegistryResponse status="urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure"
    xmlns:tns="urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0"
    xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
    <tns:RegistryErrorList highestSeverity="">
        <tns:RegistryError codeContext="XDSInvalidRequest - DcoumentId is not unique."
            errorCode="XDSInvalidRequest"
            severity="urn:oasis:names:tc:ebxml-regrep:ErrorSeverityType:Error"/>
    </tns:RegistryErrorList>
 </tns:RegistryResponse>

要检索RegistryErrorList元素,我们可以这样做

XDocument doc = XDocument.Load(<path to xml file>);
XNamespace ns = "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0";
XElement errorList = doc.Root.Elements( ns + "RegistryErrorList").SingleOrDefault();

但不是这样的

XElement errorList = doc.Root.Elements("RegistryErrorList").SingleOrDefault();

有没有办法在没有元素名称空间的情况下进行查询.基本上有一些特别的东西
类似于在XPath中使用local-name()(即// * [local-name()=’RegistryErrorList’])

var q = from x in doc.Root.Elements()
        where x.Name.LocalName=="RegistryErrorList"
        select x;

var errorList = q.SingleOrDefault();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读