【问题标题】:How do i read a XML file with namespaces (VB.net)如何读取带有命名空间的 XML 文件(VB.net)
【发布时间】:2013-06-07 10:13:53
【问题描述】:

好的,由于我关于这个问题的上一个问题有点错误,我决定制作一个更正确的新问题。

我正在尝试读取如下所示的 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>

当我使用此代码循环报告时,它没有给我任何错误,但它给了我 null;

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 文件时,给我一个关于命名空间的错误。而且确实感觉这样做是错误的方式。

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

【问题讨论】:

    标签: xml vb.net xml-parsing linq-to-xml xml-namespaces


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多