1

我正在尝试创建一个循环,该循环读取具有特定 ID 号的项目的 XML 文档,然后将该项目的子节点存储为变量,然后用于填充我页面上的标签。

Sub LinkButton_Click(sender As Object, e As CommandEventArgs)
    Dim selected = Convert.ToInt32(e.CommandArgument)
    System.Diagnostics.Debug.WriteLine("selected")
    System.Diagnostics.Debug.WriteLine (selected)

    Dim selectedXML = New XmlTextReader(MapPath("xml/questions.xml"))

    selectedXML.ReadToFollowing("Question")
    If selectedXML.HasAttributes Then
        selectedXML.MoveToNextAttribute()

        Do While selectedXML.MoveToNextAttribute()

            Dim compareID = selectedXML.Value
            System.Diagnostics.Debug.WriteLine("compareID:")
            System.Diagnostics.Debug.WriteLine(compareID)

            If compareID = selected Then
                selectedXML.ReadToFollowing("A")
                sA.Text = selectedXML.Value
                System.Diagnostics.Debug.WriteLine("SA:")
                System.Diagnostics.Debug.WriteLine(sA.Text)

                selectedXML.ReadToFollowing("Q")
                sQ.Text = selectedXML.Value
                System.Diagnostics.Debug.WriteLine("SQ:")
                System.Diagnostics.Debug.WriteLine(sQ.Text)

                selectedXML.ReadToFollowing("Download")
                sDL.Text = selectedXML.Value
                System.Diagnostics.Debug.WriteLine("SDL:")
                System.Diagnostics.Debug.WriteLine(sDL.Text)

                selectedXML.Close()
            Else
                selectedXML.ReadToNextSibling("Question")
            End If
        Loop
    Else
        System.Diagnostics.Debug.WriteLine("error")
        selectedXML.Close()
    End If

End Sub

XML 文档(冗余的 ID 节点用于获取 ID 号,但最终会被淘汰。当前代码只查找“id”属性。这是可能的,因为它区分大小写)

<?xml version="1.0" encoding="utf-8" ?>
<Questions>

  <Question id="1">
    <ID>1</ID>
    <Category>Troubleshooting</Category>
    <Keywords>tags troubleshooting troubleshoot slow computer slowly</Keywords>
    <Q>Why is my computer so slow?</Q>
    <A>You have bad habits and your computer sucks</A>
    <Download>None.</Download>
  </Question>

  <Question id="2">
    <ID>2</ID>
    <Category>Troubleshooting</Category>
    <Keywords>tags troubleshooting troubleshoot slow computer slowly</Keywords>
    <Q>Why is my computer so slow? (again)</Q>
    <A>You have bad habits and your computer sucks</A>
    <Download>None.</Download>
  </Question>

  <Question id="3">
    <ID>3</ID>
    <Category>Microsoft Office</Category>
    <Keywords>tags microsoft office outlook calendar room rooms meeting schedule scheduling reserving reserve</Keywords>
    <Q>How do I reserve rooms and set up meetings?</Q>
    <A>View the following Document:</A>
    <Download><![CDATA[<a href="doc/new_employee/new_employee_agreement.doc">New Employee Software and Hardware Agreement</a>]]></Download>
  </Question>

</Questions>

链接按钮将变量“selected”作为命令参数传递。在我的控制台中,我可以看到它工作正常。即,单击第一项将“选定”设置为 1。我遇到的问题是读取 XML 并将 id 属性作为compareID变量传递。不管我做什么,我似乎总是得到compareID=0

其次,循环似乎无限期地运行。在找不到匹配项后,如何对阅读器进行编程以使其在到达文档末尾时停止。

4

1 回答 1

0

即使出现异常,也可以使用Using 语句自动关闭 XmlReader。

不要使用XmlTextReader类,建议从 .NET Framework 2.0 开始。请改用XmlReader

请注意,我们必须转向节点QA并且Download按照它们在 xml 文档中的排列顺序。

简化代码:

Using selectedXML = XmlReader.Create(MapPath("xml/questions.xml"))
    While selectedXML.ReadToFollowing("Question") ' Iterates through Question nodes.

        Dim compareID = selectedXML.GetAttribute("id")

        If compareID IsNot Nothing Then                
            Debug.WriteLine("compareID: " + compareID)

            If compareID = selected Then
                selectedXML.ReadToFollowing("Q")
                sQ.Text = selectedXML.ReadElementContentAsString
                Debug.WriteLine("SQ: " + sQ.Text)

                selectedXML.ReadToFollowing("A")
                sA.Text = selectedXML.ReadElementContentAsString
                Debug.WriteLine("SA: " + sA.Text)

                selectedXML.ReadToFollowing("Download")
                sDL.Text = selectedXML.ReadElementContentAsString
                Debug.WriteLine("SDL: " + sDL.Text)

                Exit While
            End If
        Else
            Debug.WriteLine("Error: attribute id not found")
        End If

    End While
End Using ' Close XmlReader
于 2015-10-06T20:39:17.170 回答