繁体   English   中英

我如何读取带有名称空间的XML文件(VB.net)

[英]How do i read a XML file with namespaces (VB.net)

好的,因为我对这个问题的最后一个问题有点毛病,所以我决定重新编写一个更正确的问题。

我试图读取一个看起来像这样的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"> 
  <AllThingsInThisDocument> 
    <Headerstuff>
    </Headerstuff>
    <Reports>
      <Report>
        <Id>01</Id>
        <Name>AA</Name>
      </Report>
      <Report>
        <Id>02</Id>
        <Name>BB</Name>
      </Report>
    </Reports>
  </AllThingsInThisDocument>
</Document>

当我使用此代码循环报告时,它没有错误,但为空;

Dim xmlr As XDocument = XDocument.Load("MyMxlFile.xml")
For Each report As XElement In xmlr.Descendants("Report")
   'Do some cool stuff
Next

我发现正是xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"导致了我的错误。 如果我从xmlfile中删除它,它会给我报告。

仅出于测试目的,我尝试了两个函数,一个函数删除所有名称空间,另一个函数将它们设置为空白。 我在循环之前运行了这些功能。 这可行,但是当我尝试将名称空间另存为新的xml文件时,却给了我关于名称空间的错误。 确实感觉这样做是错误的方式。

如何使用此命名空间读取这些报告?

Dim df As XNamespace = xmlr.Root.Name.Namespace

For Each report As XElement In xmlr.Descendants(df + "Report")
  Console.WriteLine(report.Element(df + "Name").Value)
Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM