使用Visual Basic解析xml文件

时间:2020-01-15 21:33:54

标签: xml vb.net parsing

我正在尝试解析以下示例xml文件。 我无法从文件中获取xrefid =和callout =文本。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE etm PUBLIC "-//IADS//DTD Patriot Publications (patiads4.dtd)//EN" "patiads4.dtd"[]>
<etm id="n28_1b0n_pc001" label="1">
  <chapter label="1">
    <title>
      INTRODUCTION
    </title>
     <para0.outline>
        <para>
          <xref xrefid="n28_1b0n_wp000100" callout="0001 00 General Information" />
        </para>
     </para0.outline>
     <para0.outline>
        <para>
          <xref xrefid="n28_1b0n_wp000200" callout="0002 00 Nomenclature Cross-Reference" />
        </para>
     </para0.outline>
     <para0.outline>
        <para>
           <xref xrefid="n28_1b0n_wp000300" callout="0003 00 Differences Between Models" />
        </para>
     </para0.outline>
     <para0.outline>
        <para>
           <xref xrefid="n28_1b0n_wp000400" callout="0004 00 Equipment Description" />
        </para>
     </para0.outline>
  </chapter>
</etm>

这是我正在使用的代码:

   Imports System.IO
   Imports System.Xml

   Public Class Form1
       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

         If (ComboBox1.Text = "") Then 

             MessageBox.Show("No file name entered")

         Else

             If (System.IO.File.Exists(ComboBox1.Text.ToString())) Then

                 Dim document As XmlReader = New XmlTextReader(ComboBox1.Text.ToString())

                 While (document.Read())

                     Dim type = document.NodeType

                     If (type = XmlNodeType.Element) Then

                         If (document.Name = "title") Then
                              xmlTitle.Visible = True
                              xmlTitle.Text = document.ReadInnerXml.ToString()
                         End If
                         If (document.Name.Contains("para")) Then

                              Dim test As String = document.ReadInnerXml.ToString()
                              Dim Doc As New XmlDocument()
                              Doc.LoadXml("<para>" & test & "</para>")

                              Dim root As XmlNode = Doc.FirstChild

                              If root.HasChildNodes Then
                                  Dim _value = root.ChildNodes.Item(0).InnerText.Trim()

                                  xmlRefId1.Visible = True
                                  xmlRefId1.Text = _value
                              End If
                         End If
                     End If
                 End While
             Else
                MessageBox.Show("The filename you selected was not found.")
             End If
         End If
       End Sub
   End Class

0 个答案:

没有答案
相关问题