簡體   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