读取XML文件并在文本框中显示值时出现问题

时间:2013-05-28 17:33:23

标签: vb.net visual-studio-2012 visual-studio-express

我正在尝试为我的Windows窗体设置一个设置区域,我可以为我的应用程序保存4组设置。

写入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文件,并且被认为是“黑暗时代”。

我愿意接受建议。我的表格如下所示:

settingsform

非常感谢任何帮助或示例代码。

2 个答案:

答案 0 :(得分:0)

您实际上并未在任何地方设置文本框的值。看起来您将.Text属性作为参数传递给.ReadString()方法,但该方法是否会接受参数?见MSDN。根据该链接,.ReadString() 从XML文件返回字符串。因此,您需要将文本框设置为返回值:

CDtxtbx1.Text = XMLR.ReadString()

答案 1 :(得分:0)

根据this MSDN page,您为什么不尝试以下操作:

XMLR.ReadStartElement("code1")
CDtxtbx1.text=XMLR.ReadString()
XMLR.ReadEndElement()