简体   繁体   English

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

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

Ok, since my last question about this trouble was a bit faulty, i decided to make a new one that's more correct. 好的,因为我对这个问题的最后一个问题有点毛病,所以我决定重新编写一个更正确的问题。

Im trying to read a XML file that looks like this: 我试图读取一个看起来像这样的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>

when i use this code to loop the reports, it gives me no errors but it gives me null; 当我使用此代码循环报告时,它没有错误,但为空;

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

I have found out that it's the xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" that gives me the error. 我发现正是xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"导致了我的错误。 If i remove this from the xmlfile, it gives me the reports. 如果我从xmlfile中删除它,它会给我报告。

Just for test, I've tried with two functions, one that removes all namespaces and one that sets them to blank. 仅出于测试目的,我尝试了两个函数,一个函数删除所有名称空间,另一个函数将它们设置为空白。 I ran these function before looping. 我在循环之前运行了这些功能。 This works, but gives me an error about the namespace when i try to save it as a new xml file. 这可行,但是当我尝试将名称空间另存为新的xml文件时,却给了我关于名称空间的错误。 And it really feels like the wrong way of doing this. 确实感觉这样做是错误的方式。

How can i read these reports with this namespace? 如何使用此命名空间读取这些报告?

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