LINQ到XML与空间的XDocument问题 [英] Linq-to-XML with XDocument namespace issue
问题描述
我有这样的XML文档:
I have this XML document:
<?xml version="1.0" encoding="utf-8"?>
<directoryresponse xmlns="https://www.sisow.nl/Sisow/REST" version="1.0.0">
<directory>
<issuer>
<issuerid>01</issuerid>
<issuername>ABN Amro Bank</issuername>
</issuer>
<issuer>
<issuerid>02</issuerid>
<issuername>ASN Bank</issuername>
</issuer>
</directory>
</directoryresponse>
和这不起作用:
var banks = doc.Descendants("issuer").Select(x =>
new Bank(Convert.ToInt32(x.Element("issuerid").Value), x.Element("issuername").Value)).ToList();
但是,当我手动删除该directoryresponse XML命名空间
的xmlns = https://www.sisow.nl/Sisow/REST
它的作品!命名空间的网址是一个 404 。 ?那么,为什么不将 XDOC
忽略的命名空间,如果它是一个404
But when I manual remove the directoryresponse xml namespace
xmlns="https://www.sisow.nl/Sisow/REST"
it works! The namespace url is a 404. So why doesn't the xdoc
ignore the namespace if it's a 404?
这也不起作用: VAR银行= doc.Elements()式(E => e.Name.LocalName ==发行人)。。选择(...
的主要问题是:我怎么可以修改我的代码,以便它忽略了404命名空间
The main question is: how can I modify my code so that it ignores the 404 namespace?
推荐答案
URL本身在这里无关紧要 - 它只是为命名空间的道理,我真的不相信的LINQ to XML将尝试把它拿来
The URL itself is irrelevant here - it's just a token for the namespace, really. I don't believe LINQ to XML will try to fetch it.
不过。 ,你需要用它来构建的XName
搜索:
However, you need to use it to construct the XName
to search for:
XNamespace ns = "https://www.sisow.nl/Sisow/REST";
var banks = doc.Descendants(ns + "issuer")
.Select(x => new Bank((int) x.Element(ns + "issuerid"),
(string) x.Element(ns + "issuername"))
.ToList();
这篇关于LINQ到XML与空间的XDocument问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!