2015-10-06 43 views
1

我試圖做一個循環,讀取具有特定ID號的項目的XML文檔,然後將此項目的子節點存儲爲變量,然後用它來填充我的頁面上的標籤。XmlTextReader無法正常工作

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> 

linkbutton通過變量「selected」作爲命令參數。在我的控制檯中,我可以看到這是正常工作。即單擊第一個項目,將「selected」設置爲1.在遇到問題時,請閱讀XML並將id屬性作爲compareID變量。無論我做什麼,我似乎總是得到compareID=0

其次,循環似乎無限期地運行。在找不到匹配項後,如何讓閱讀器在文檔結束時停止編程。

回答

0

使用Using Statement即使在例外的情況下也會自動關閉XmlReader。使用XmlTextReader類,建議不要使用.NET Framework 2.0開始。改爲使用XmlReader

請注意,我們必須按照排列在xml文檔中的順序轉到節點QADownload

簡化代碼:

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 
+0

偉大的作品!謝謝 – MRG