2017-03-22 69 views
0

我想閱讀我的XML的特定節點,但我在如何關注問題方面遇到問題。使用隨機Id節點獲取XML數據

眼下XML文件中有這樣的結構:

<?xml version="1.0" encoding="utf-8"?> 
<Assessment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www1.macadam.eu/Webservices/FleetCarV3/"> 
<Damages> 
<Damage> 
    <Id>15724669</Id> 
    <AssessmentId>1959065</AssessmentId> 
    <Location> 
    <Id>5</Id> 
    <DescriptionEN>Panel R R</DescriptionEN> 
    <DescriptionNL>Panneel A R</DescriptionNL> 
    <DescriptionFR>Panneau ARD</DescriptionFR> 
    <DescriptionDE>Panel HR</DescriptionDE> 
    <DescriptionES>Panel TRAS DR</DescriptionES> 
    </Location> 
    <Type> 
    <Id>1274</Id> 
    <DescriptionEN>Bump(s)</DescriptionEN> 
    <DescriptionNL>Deuk(en)</DescriptionNL> 
    <DescriptionFR>Bosse(s)</DescriptionFR> 
    <DescriptionDE>Dell(en)</DescriptionDE> 
    <DescriptionES>Abolladuras</DescriptionES> 
    </Type> 
    <Repair> 
    <Id>1307</Id> 
    <DescriptionEN>Le - Replace and paint</DescriptionEN> 
    <DescriptionNL>Le - Vervangen en spuiten</DescriptionNL> 
    <DescriptionFR>Le - Remplacer et peindre</DescriptionFR> 
    <DescriptionDE>Erneuern + lackieren</DescriptionDE> 
    <DescriptionES>Le - Reemplazar Y Pintar</DescriptionES> 
    </Repair> 
    <Comment /> 
</Damage> 
<Damage> 
    ... 
    ... 
</Damage> 
<Damage> 
    ... 
    ... 
</Damage> 
</Damages> 
</Assessment> 

注意,我把更多的<Damage>標籤。每個<Id>對於每個<Damage>節點都是不同的,我需要的是<DescriptionES>節點中的文本。

下面的代碼試圖讓<Location>節點:

nsmgr = New XmlNamespaceManager(m_xmld.NameTable) 
nsmgr.AddNamespace("sd", "http://www1.macadam.eu/Webservices/FleetCarV3/") 

m_nodelist = m_xmld.DocumentElement.SelectNodes("/sd:Assessment/sd:Damages/sd:Damage/sd:Location", nsmgr) 
For Each m_node In m_nodelist 
    For j = 0 To 5000 
     Dim Location = m_node.SelectSingleNode("/sd:Assessment/sd:Damages/sd:Damage/sd:Location[sd:Id=" & j & "]", nsmgr).InnerText 
     If Location IsNot Nothing Then 
      MsgBox("i'm in") 
      'txtDamageReport.Text = Location 
     End If 
    Next j 
Next 

它不工作,符合市場預期。我不知道如何繼續。

+0

您的代碼似乎並沒有針對特定的節點。你想獲得所有傷害的所有位置嗎? – chateaur

+0

@chateaur這是一個測試,看看我是否可以訪問。我需要全部。 – smtdev

回答

1

您可以使用LINQ到XML的。沒有什麼競價易於理解的API和更好的可讀性

Dim document As XDocument = XDocument.Load(pathToTheFile) 
Dim ns = XNamespace.Get("http://www1.macadam.eu/Webservices/FleetCarV3/") 

For Each damage As XElement in document.Descendants(ns + "Damage") 
    Dim damageId = damage.Element(ns + "Id").Value 
    Dim locationDescription = damage.Element(ns + "Location"). 
            Element(ns + "DescriptionES"). 
            Value 

    MessageBox.Show($"Id = {damageId}; Desc = {locationDescription}") 
Next 

vb.net有可能使用XML Axis Properties,它可以是有用的,如果您對XML文件的架構,那麼您將獲得對元素名稱的IntelliSense支持。
使用Axis屬性與上面相同的代碼。
對於使用命名空間軸屬性,你需要導入的命名空間的文件

Imports <xmlns:ns="http://www1.macadam.eu/Webservices/FleetCarV3/"> 

For Each damage As XElement in document...<ns:Damage> 
    Dim damageId = damage.<ns:Id>.Value 
    Dim locationDescription = damage.<ns:Location>.<ns:DescriptionES>.Value 

    MessageBox.Show($"Id = {damageId}; Desc = {locationDescription}") 
Next 
+0

感謝您的回答。我正在使用你的代碼,但它沒有顯示任何彈出(都已經嘗試過)。它看起來非常清晰和邏輯代碼,但沒有顯示數據。這有點奇怪。 – smtdev

+0

你也需要證明一個命名空間,檢查我更新的答案 – Fabio

+0

哇,它現在就像一個魅力。非常感謝您的解決方案以及LINQ和Xml Properties的微型教程。當然,我會在將來使用它。 – smtdev

0

您可以使用VB的XmlDocument如下。我相信,XmlNamespaceManager的是錯誤的代碼,但因爲你沒有提供它...

這個工作對我來說:

Dim doc As New XmlDocument 

doc.Load(path) 

//Add a namespace to all the document 
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable) 
     nsmgr.AddNamespace("g", "http://www1.macadam.eu/Webservices/FleetCarV3/") 

Dim nodeList As XmlNodeList 
Dim root As XmlElement = doc.DocumentElement 
nodeList = root.SelectNodes("//g:Assessment//g:Damages//g:Damage//g:Location", nsmgr) 

// Loop over damage section 
For Each damageNode As XmlNode In nodeList 
    Dim id As String = damageNode.SelectSingleNode("//g:Id", nsmgr).InnerText 
    Dim desc As String = damageNode.SelectSingleNode("//g:DescriptionES", nsmgr).InnerText 

     MsgBox(id & vbCrLf & desc) 
Next 
+0

命名空間已添加!就我而言,您的代碼獲取的數據與部分不同。問題是這是一個〜3k行的XML,所以我想它有一些麻煩獲取數據。展示完整的XML內容可能是一個好主意。 – smtdev