在VB.Net中解析包含名称空间的XML

时间:2017-03-13 22:34:25

标签: xml vb.net xml-parsing

我发现了几个相关的问题,但没有一个能让我找到办法让它发挥作用。

我有一个具有以下结构的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAssessmentItemTimed xmlns:xsi="http://a.b.c/" xmlns:xsd="http://d.e.f" xmlns="http://g.h.i">
  <AssessmentItemTimed>
    <CustomerId>306</CustomerId>
    <AssessmentId>1959060</AssessmentId>
    <ExternalReference />
    <License>0577HJK</License>
    <ChassisNumber>VF1KT1RG646667276</ChassisNumber>
    <CostTraditional>2771.16</CostTraditional>
    <CostCommercial>2771.16</CostCommercial>
    <Kilometers>90957</Kilometers>
    <TimeOrdered>2017-02-13T16:31:00.48</TimeOrdered>
    <TimeClosed>2017-02-17T17:09:43.14</TimeClosed>
  </AssessmentItemTimed>
  <AssessmentItemTimed>
    <CustomerId>306</CustomerId>
    <AssessmentId>1959061</AssessmentId>
    <ExternalReference />
    <License>0918HHM</License>
    <ChassisNumber>VF1KT1RG646272649</ChassisNumber>
    <CostTraditional>3293.33</CostTraditional>
    <CostCommercial>3293.33</CostCommercial>
    <Kilometers>158142</Kilometers>
    <TimeOrdered>2017-02-13T16:31:01.73</TimeOrdered>
    <TimeClosed>2017-02-16T21:05:38.98</TimeClosed>
  </AssessmentItemTimed>
 </ArrayOfAssessmentItemTimed>

如果从标记中删除命名空间,我只能获取所需的数据。这是我目前的代码:

    Dim xmlnode As XmlDocument
    Dim nodeList As XmlNodeList
    Dim node As XmlNode
    Dim nsmgr As XmlNamespaceManager

    Try

        xmlnode = New XmlDocument()
        xmlnode.Load("file.xml")
        nsmgr = New XmlNamespaceManager(xmlnode .NameTable)

        nodeList = xmlnode.DocumentElement.SelectNodes("/sd:ArrayOfAssessmentItemTimed/sd:AssessmentItemTimed", nsmgr)

        For Each xmlnode In nodeList 

            Dim plateNumber = xmlnode.SelectSingleNode("sd:License", nsmgr).InnerText
            Dim chassisNumber = xmlnode.SelectSingleNode("sd:ChassisNumber", nsmgr).InnerText
            Dim Kilometers = mlnode.SelectSingleNode("sd:Kilometers", nsmgr).InnerText

            'test if works
            MsgBox("License: " & plateNumber & vbCrLf _
                       & "Chassis: " & chassisNumber & vbCrLf _
                       & "Kms: " & Kilometers & vbCrLf & vbCrLf
                       )
        Next
    Catch errorVariable As Exception
        'Error trapping
        MsgBox(errorVariable.ToString())
    End Try

这可能是排除xmlns值的正确方法?我真的不了解XmlNamespaceManager,所以我无法获得积极的结果。

感谢您的所有建议。

2 个答案:

答案 0 :(得分:1)

您需要先使用XmlNamespaceManager注册前缀'sd',然后才能在XPath查询中使用它。确保将前缀映射到默认命名空间http://g.h.i

....
nsmgr = New XmlNamespaceManager(New XmlNameTable())
nsmgr.AddNamespace("sd", "http://g.h.i");

nodeList = xmlnode.DocumentElement.SelectNodes("/sd:ArrayOfAssessmentItemTimed/sd:AssessmentItemTimed", nsmgr)
....

XPath总是将没有前缀的元素视为具有空命名空间的元素,它没有默认命名空间的概念(只有XML),这就是为什么你需要在这里组成前缀'sd'。

答案 1 :(得分:0)

您可以查看How to: Declare and Use XML Namespace Prefixes以获得更简单的替代方案。

'' Dim xDoc = System.Xml.Linq.XDocument.Load("file.xml")

Dim xDoc = <?xml version="1.0" encoding="utf-8"?>
           <ArrayOfAssessmentItemTimed xmlns:xsi="http://a.b.c/"
               xmlns:xsd="http://d.e.f" xmlns="http://g.h.i">
               <AssessmentItemTimed>
                   <License>0577HJK</License>
                   <ChassisNumber>VF1KT1RG646667276</ChassisNumber>
                   <Kilometers>90957</Kilometers>
               </AssessmentItemTimed>
               <AssessmentItemTimed>
                   <License>0918HHM</License>
                   <ChassisNumber>VF1KT1RG646272649</ChassisNumber>
                   <Kilometers>158142</Kilometers>
               </AssessmentItemTimed>
           </ArrayOfAssessmentItemTimed>

For Each x In xDoc.<ArrayOfAssessmentItemTimed>.<AssessmentItemTimed>

    MsgBox("License: " & vbTab & x.<License>.Value & vbLf &
           "Chassis: " & vbTab & x.<ChassisNumber>.Value & vbLf &
           "Kms:     " & vbTab & x.<Kilometers>.Value & vbLf)
Next

和样品使用:

{{1}}