[英]Adding element to existing XML file with LINQ in VB.NET
在加載時,我的程序執行以下代碼來確定是否已存在XML文件,如果不存在,則創建一個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
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.