讓我們假設我們有這個XML:有沒有辦法在Linq-to-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();
有沒有辦法做查詢沒有元素的名稱空間。 Basicly是有什麼conceptially 類同XPath中使用本地名稱()(即// * [本地名稱()= 'RegistryErrorList'])