解析 XDocument C# 中的命名空间
我正在从包含几个命名空间的 XML 文件导入一些数据。我正在使用嵌套类来导入不同级别的数据。不幸的是我无法访问内部类中的数据。我知道问题与名称空间有关,但我不知道如何在不修改内部类的情况下解决它们。有谁知道我如何解析 XDocument 中的名称空间。
这是该 XML 的一部分:
<TransXChange xmlns="http://www.transxchange.org.uk/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:apd="http://www.govtalk.gov.uk/people/AddressAndPersonalDetails"
xsi:schemaLocation="http://www.transxchange.org.uk/ http://www.transxchange.org.uk/schema/2.1/TransXChange_registration.xsd"
xml:lang="en" CreationDateTime="2004-06-09T14:20:00-05:00"
>
<Operators>
<LicensedOperator id="O1" >
<NationalOperatorCode>ABC</NationalOperatorCode>
<OperatorAddresses>
<CorrespondenceAddress>
<apd:Line>45 City Road</apd:Line>
</CorrespondenceAddress>
</OperatorAddresses>
</LicensedOperator>
</Operators>
</TransXChange>
I am importing some data from an XML file which contains a couple of namespaces. I am using nested classes to import data in different level. Unfortunately I cannot access the data in inner classes. I know the problem is regarding namespaces but I do not know how I can resolve them without modifying inner classes. Does anybody know how I can resolve namespaces in XDocument.
This is a part of that XML:
<TransXChange xmlns="http://www.transxchange.org.uk/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:apd="http://www.govtalk.gov.uk/people/AddressAndPersonalDetails"
xsi:schemaLocation="http://www.transxchange.org.uk/ http://www.transxchange.org.uk/schema/2.1/TransXChange_registration.xsd"
xml:lang="en" CreationDateTime="2004-06-09T14:20:00-05:00"
>
<Operators>
<LicensedOperator id="O1" >
<NationalOperatorCode>ABC</NationalOperatorCode>
<OperatorAddresses>
<CorrespondenceAddress>
<apd:Line>45 City Road</apd:Line>
</CorrespondenceAddress>
</OperatorAddresses>
</LicensedOperator>
</Operators>
</TransXChange>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用与 XML 中使用的命名空间相匹配的命名空间,对于 Linq to XML,您可以使用
XNamespace
来实现这一点,即:You have to use the namespace that matches the one used in your XML, with Linq to XML you can use
XNamespace
for that i.e.:您需要告诉解析器如何引用名称空间:
应该可以做到
You need to tell the parser how to reference the namespace:
That should do it