2013-10-09 36 views
0

在負載我的程序執行以下代碼,以確定是否一個XML文件已經存在,如果沒有,創建一個:添加元素與LINQ現有的XML文件中的VB.NET

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 

回答

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)) 
相關問題