我想阅读我的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 :(得分:1)
您可以使用LINQ to 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
答案 1 :(得分: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