如何从选定的ComboBox项目中的XML文件正确填充文本框?

时间:2018-06-19 16:07:26

标签: xml vb.net combobox xml-parsing

我尝试了许多不同的方法来使它起作用,并且它一直在加载第一个项目。一旦文本与组合框匹配,我就需要能够用XML文件中的所有项目填充各种文本框。

基本上,我加载我的应用程序,首先读取XML文件来加载ComboBox,然后当我从ComboBox中选择一个项目时,我会加载其他带有错误详细信息的文本框。

我的XML文件:

<?xml version="1.0" encoding="us-ascii"?>
<!--SIP Data created by TECH on 1/24/2014 10:07:04 AM-->
<ERROR_ITEMS>
    <ERROR_DATA Posted="6/11/2018 12:00:00 PM">
        <Response>ERROR001</Response>
            <Method>ErrorCode001</Method>
                <Request>Acknowledged</Request>
                    <Code>001</Code>
                        <Information>Information for code 001 goes here</Information>
                            <Troubleshooting>Error 001 designates connections failed. Check all connections.</Troubleshooting>
                                <Additional_Notes>Tech notes: Ensure proper gauge of wiring.</Additional_Notes>
                                    <DateTimeSaved>11/10/2014 1:45:25 PM</DateTimeSaved>
        <Response>ERROR002</Response>
            <Method>ErrorCode002</Method>
                <Request>Acknowledged</Request>
                    <Code>002</Code>
                        <Information>Information for code 002 goes here</Information>
                            <Troubleshooting>Error 002 designates input power failed. Check power to aux inputs.</Troubleshooting>
                                <Additional_Notes>Tech notes: Check all power connections</Additional_Notes>
                                    <DateTimeSaved>11/10/2014 1:50:25 PM</DateTimeSaved>
  </ERROR_DATA>
</ERROR_ITEMS>

还有我的VB.net代码:

Imports System.Xml
Imports System.Text
Imports System.IO
Imports System.Environment
Public Class Form1

    Private Sub ExitAppBTN_Click(sender As Object, e As EventArgs) Handles ExitAppBTN.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Try
            ' LOAD THE ERRORS INTO THE COMBO BOX
            Dim document As XmlDocument = New XmlDocument
            document.Load(Application.StartupPath & "/items.xml")

            For Each node In document.SelectNodes("ERROR_ITEMS/ERROR_DATA/Response")
                CmboERRORList.Items.Add(node.InnerText())
            Next

            'Load First Item
            CmboERRORList.SelectedIndex = 0
            CmboERRORList.Focus()
        Catch ex As Exception
            MsgBox("Error processing file. The error was: " & vbCrLf & Err.Description, MsgBoxStyle.Exclamation, "Error loading file contents to drop down list.")
        End Try
    End Sub

        Private Sub CmboERRORList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmboERRORList.SelectedIndexChanged
                Try
                    'Load info from the XML file

                    Dim document As XmlDocument = New XmlDocument
                    document.Load(Application.StartupPath & "/items.xml")
                    Dim node = document.SelectSingleNode("ERROR_ITEMS/ERROR_DATA[Response = '" & CmboERRORList.Text & "']")
                    MsgBox(node.SelectSingleNode("Method").InnerText)

                    For Each node In document.SelectNodes("ERROR_ITEMS/ERROR_DATA[Response = '" & CmboERRORList.Text & "']")
                        Dim ErrResponseCode = node.SelectSingleNode("Method").InnerText
                        Dim ErrDetails = node.SelectSingleNode("Information").InnerText
                        txtDetails.Text = ErrDetails
                    Next

                Catch ex As Exception
                    MsgBox("Error Reading Error Data File, please contact technical support. Error is: " & Err.Description & vbCrLf & "Error Number is :" & Err.Number, MsgBoxStyle.Exclamation, "Error")
                End Try
            End Sub

1 个答案:

答案 0 :(得分:1)

使用它代替当前的ComboBox SelectedIndexChanged事件处理程序。

Private Sub CmboERRORList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmboERRORList.SelectedIndexChanged
    Try
        'Load info from the XML file

        Dim settings As New XmlReaderSettings()
        settings.DtdProcessing = DtdProcessing.Parse
        Dim reader As XmlReader = XmlReader.Create("XmlFile1.xml", settings)
        Dim found As Boolean = False
        Dim nodeName As String = String.Empty
        Dim nodeText As String = String.Empty
        While reader.Read
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    nodeName = reader.Name
                Case XmlNodeType.Text
                    nodeText = reader.Value
            End Select

            If (nodeName = "Response" AndAlso nodeText = CmboERRORList.Text) Then
                found = True
            End If

            If (found AndAlso nodeName = "Method" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Method = " & reader.Value)
            ElseIf (found AndAlso nodeName = "Request" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Request = " & reader.Value)
            ElseIf (found AndAlso nodeName = "Code" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Code = " & reader.Value)
            ElseIf (found AndAlso nodeName = "Information" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Information = " & reader.Value)
                txtDetails.Text = reader.Value
            ElseIf (found AndAlso nodeName = "Troubleshooting" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Troubleshooting = " & reader.Value)
            ElseIf (found AndAlso nodeName = "Additional_Notes" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("Additional_Notes = " & reader.Value)
            ElseIf (found AndAlso nodeName = "DateTimeSaved" AndAlso reader.NodeType = XmlNodeType.Text) Then
                Debug.WriteLine("DateTimeSaved = " & reader.Value)

                Exit While
            End If
        End While
    Catch ex As Exception
        MsgBox("Error Reading Error Data File, please contact technical support. Error is: " & Err.Description & vbCrLf & "Error Number is :" & Err.Number, MsgBoxStyle.Exclamation, "Error")
    End Try
End Sub

在这里,我正在使用XmlReader类来逐行读取Xml。如果您的Xml文档不是分层文档,那么XmlReader确实是浏览该文档的最佳选择。

XmlReader (Link to MSDN)的级别很低,这意味着您需要检查每个阶段正在阅读的内容并采取相应的措施。 More information on the Read method and how to handle the NodeTypes

请注意,我在最后一个Else..if语句中放置了一个Exit While,因为终止DateTimeSaved节点是While循环,是您感兴趣的最后一个