特定の 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>
linkbutton は、変数「selected」をコマンド引数として渡しました。私のコンソールでは、これが正しく機能していることがわかります。つまり、最初の項目をクリックすると、「選択済み」が 1 に設定されます。問題があるのは、XML を読み取って id 属性をcompareID
変数として渡すことです。私が何をしても、私はいつも得るようですcompareID=0
.
次に、ループが無期限に実行されているようです。一致が見つからなかった後、ドキュメントの最後に達したときにリーダーが停止するようにプログラムするにはどうすればよいですか。