在加载时,我的程序执行以下代码以确定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
答案 0 :(得分: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))