我想爲我的窗體設置一個設置區域,我將能夠爲我的應用程序保存4組設置。閱讀XML文件並在文本框中顯示值的問題
寫入XML文件不是問題,因爲我可以成功完成此操作,而且不會出現問題。
問題出現在讀取xml文件。本質上,我希望當用戶打開設置表單時,將xml文件中的值插入設置表單上的「特定」文本框。
是否有人能夠告訴我我做錯了什麼,如果可能的話提供一些示例代碼來幫助我使閱讀部分工作?
代碼在下面,我想添加所有的id(1到4)在設置框中讀取,現在只提供id1,直到我可以爲其他人獲得正確的代碼。
Public Class SettingsFrm
Private Sub SettingsFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rdxmlfile As String = "\\ServerA\ITDept\test\Settings.xml"
'Create the xml xmlreader
Dim XMLR As New Xml.XmlTextReader(rdxmlfile)
'Read the id1 element
XMLR.ReadStartElement("id1")
'Read the value of the code1 element and display in the appropriate textbox
XMLR.ReadStartElement("code1")
XMLR.ReadString(CDtxtbx1.Text)
XMLR.ReadEndElement()
'Read the value of the docgroup1 element and display in the appropriate textbox
XMLR.ReadStartElement("docgroup1")
XMLR.ReadString(DGtxtbx1.Text)
XMLR.ReadEndElement()
'Read the value of the doctype1 element and display in the appropriate textbox
XMLR.ReadStartElement("doctype1")
XMLR.ReadString(DTtxtbx1.Text)
XMLR.ReadEndElement()
'Read the value of the docsubtype1 element and display in the appropriate textbox
XMLR.ReadStartElement("docsubtype1")
XMLR.ReadString(DSTtxtbx1.Text)
XMLR.ReadEndElement()
'close the id1 element
XMLR.ReadEndElement()
'close cml
XMLR.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim svxmlfile As String = "\\ServerA\ITDept\test\Settings.xml"
'Create the xml writer obeject
Dim XMLW As New Xml.XmlTextWriter(svxmlfile, System.Text.Encoding.GetEncoding("UTF-8"))
'xml format to be written as:
'<id1>
' <code1> FTO </code1>
' <docgroup1> Operations </docgroup1>
' <doctype1> Funds Transfer </doctype1>
' <docsubtype1> Out </docsubtype1>
'</id1>
XMLW.Formatting = Xml.Formatting.Indented
'write id1
XMLW.WriteStartElement("id1")
' <code element>
XMLW.WriteStartElement("code1")
XMLW.WriteValue(CDtxtbx1.Text)
XMLW.WriteEndElement()
' <DocGroup element>
XMLW.WriteStartElement("docgroup1")
XMLW.WriteValue(DGtxtbx1.Text)
XMLW.WriteEndElement()
' <DocType element>
XMLW.WriteStartElement("doctype1")
XMLW.WriteValue(DTtxtbx1.Text)
XMLW.WriteEndElement()
' <DocSubType element>
XMLW.WriteStartElement("docsubtype1")
XMLW.WriteValue(DSTtxtbx1.Text)
XMLW.WriteEndElement()
'the end of id1
XMLW.WriteEndElement()
'write id2
XMLW.WriteStartElement("id2")
' <code element>
XMLW.WriteStartElement("code2")
XMLW.WriteValue(CDtxtbx2.Text)
XMLW.WriteEndElement()
' <DocGroup element>
XMLW.WriteStartElement("docgroup2")
XMLW.WriteValue(DGtxtbx2.Text)
XMLW.WriteEndElement()
' <DocType element>
XMLW.WriteStartElement("doctype2")
XMLW.WriteValue(DTtxtbx2.Text)
XMLW.WriteEndElement()
' <DocSubType element>
XMLW.WriteStartElement("docsubtype2")
XMLW.WriteValue(DSTtxtbx2.Text)
XMLW.WriteEndElement()
'the end of id2
XMLW.WriteEndElement()
'write id3
XMLW.WriteStartElement("id3")
' <code element>
XMLW.WriteStartElement("code3")
XMLW.WriteValue(CDtxtbx3.Text)
XMLW.WriteEndElement()
' <DocGroup element>
XMLW.WriteStartElement("docgroup3")
XMLW.WriteValue(DGtxtbx3.Text)
XMLW.WriteEndElement()
' <DocType element>
XMLW.WriteStartElement("doctype3")
XMLW.WriteValue(DTtxtbx3.Text)
XMLW.WriteEndElement()
' <DocSubType element>
XMLW.WriteStartElement("docsubtype3")
XMLW.WriteValue(DSTtxtbx3.Text)
XMLW.WriteEndElement()
'the end of id3
XMLW.WriteEndElement()
'write id4
XMLW.WriteStartElement("id4")
' <code element>
XMLW.WriteStartElement("code4")
XMLW.WriteValue(CDtxtbx4.Text)
XMLW.WriteEndElement()
' <DocGroup element>
XMLW.WriteStartElement("docgroup4")
XMLW.WriteValue(DGtxtbx4.Text)
XMLW.WriteEndElement()
' <DocType element>
XMLW.WriteStartElement("doctype4")
XMLW.WriteValue(DTtxtbx4.Text)
XMLW.WriteEndElement()
' <DocSubType element>
XMLW.WriteStartElement("docsubtype4")
XMLW.WriteValue(DSTtxtbx4.Text)
XMLW.WriteEndElement()
'the end of id4
XMLW.WriteEndElement()
'close the element
XMLW.Close()
MessageBox.Show("Settings Saved")
End Sub
End Class
我很新的VB.net和編程一般,所以如果有一個更簡單的方法來做我想做的事情,那麼我都耳熟能詳。我首先想到可能使用INI文件來存儲我的設置表單的設置,但在閱讀了幾個論壇後,我發現ini文件不再使用,並被認爲是「黑暗時代」。
我很樂意提供建議。我的形式看起來象下面這樣:
任何幫助或示例代碼將是非常讚賞。