0

在加载时,我的程序执行以下代码来确定 XML 文件是否已经存在,如果不存在,则创建一个:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If IO.File.Exists("Dictionary.xml") = False Then

        Dim Dictionary As XDocument = <?xml version="1.0" encoding="utf-8"?>
                                      <Root></Root>

            MessageBox.Show("XML dictionary file created.")
    End If
End Sub

然后,我尝试从 4 个文本框中获取用户输入,以将每个单词附加到此 xml 文件中。我到目前为止,但找不到一个很好的例子来说明如何做到这一点。

    Private Sub Save_Data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_Data.Click

    Dim Dictionary As XDocument = XDocument.Load("Dictionary.xml")
    Dictionary.Add
    <Word>
        <English>Textbox1.Text</English>
        <Transcription>Textbox2.Text</Transcription>
        <Meaning>Textbox3.Text</Meaning>
        <Sound>Textbox4.Text</Sound>
    </Word>

End Sub
4

1 回答 1

1
Dictionary.Root.Add( _
    New XElement("Word",Textbox1.Text, _
        New XElement("English",Textbox1.Text), _
        New XElement("Transcription",Textbox2.Text), _ 
        New XElement("Meaning",Textbox3.Text), _ 
        New XElement("Sound",Textbox4.Text))
于 2013-10-09T09:38:38.720 回答