微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

让我们假设我们有这个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();

原文地址:https://www.jb51.cc/xml/292553.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。