Linq to xml 有没有一种方法可以在Linq到XML查询中仅使用本地名称检索元素?
假设我们有以下xml:Linq to xml 有没有一种方法可以在Linq到XML查询中仅使用本地名称检索元素?,linq-to-xml,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-r
<?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>
是否有一种方法可以在不使用元素名称空间的情况下执行查询。基本上有什么概念上的东西吗
类似于在XPath中使用local-name()(即/*[local-name()='RegistryErrorList'])在“方法”语法中,查询如下所示:
XElement errorList = doc.Root.Elements().Where(o => o.Name.LocalName == "RegistryErrorList").SingleOrDefault();
以下扩展将从XDocument(或任何XContainer)的任何级别返回匹配元素的集合
XElement errorList = doc.Root.Elements("RegistryErrorList").SingleOrDefault();
var q = from x in doc.Root.Elements()
where x.Name.LocalName=="RegistryErrorList"
select x;
var errorList = q.SingleOrDefault();
XElement errorList = doc.Root.Elements().Where(o => o.Name.LocalName == "RegistryErrorList").SingleOrDefault();
public static IEnumerable<XElement> GetElements(this XContainer doc, string elementName)
{
return doc.Descendants().Where(p => p.Name.LocalName == elementName);
}
var errorList = doc.GetElements("RegistryErrorList").SingleOrDefault();