2013-10-09 44 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)) 
相关问题