2015-10-20 57 views
0

我有以下的VB.NET代碼,並期待combobox1的內容被寫入到XML文件,但它什麼都不?寫組合框選定的項目到XML文件

  'Creating XML file first... 
     Dim objWriter As New System.IO.StreamWriter(IO.Directory.GetCurrentDirectory & "\PROFILE.xml") 

     objWriter.WriteLine("<COMPELATION>") 
     objWriter.WriteLine("</COMPELATION>") 
     objWriter.Close() 

     'Writing XML content... 
     Dim xmldoc As XmlDocument = New XmlDocument() 
     xmldoc.Load(IO.Directory.GetCurrentDirectory & "\PROFILE.xml") 

     With xmldoc.SelectSingleNode("/COMPELATION").CreateNavigator().AppendChild() 
      .WriteStartElement("DATA") 
      .WriteElementString("PROJECTID", TextBox1.Text) 
      .WriteElementString("CLIENTID", ComboBox1.SelectedText) 
      .WriteElementString("CDATE", Date.Today) 
      .WriteElementString("CTIME", Now.ToShortTimeString) 

      .WriteEndElement() 
      .Close() 
     End With 
     xmldoc.Save(IO.Directory.GetCurrentDirectory & "\PROFILE.xml") 

     Dim filepath As String = (IO.Directory.GetCurrentDirectory & "\PROFILE.xml") 
     Dim DS_PROJECTS As New DataSet 
     DS_PROJECTS.ReadXml(filepath) 
    End If 

回答

0

ComboBox1.SelectedText表示突出顯示的內容。執行代碼時,可能沒有用藍色突出顯示的項目。因此CLIENTID標籤中沒有插入任何內容。使用Combobox1.Text而不是代表您選擇的內容。

.WriteElementString("CLIENTID", ComboBox1.Text) 
0

我修改了你從combobox和文本框代碼硬編碼值。然後我把結果放到一個DataGridView中,它似乎工作。

Imports System.Xml 
 
Public Class Form1 
 
    Const FILENAME As String = "c:\temp\test.xml" 
 
    Sub New() 
 

 
     ' This call is required by the designer. 
 
     InitializeComponent() 
 

 
     ' Add any initialization after the InitializeComponent() call. 
 
     Dim objWriter As New System.IO.StreamWriter(FILENAME) 
 

 
     objWriter.WriteLine("<COMPELATION>") 
 
     objWriter.WriteLine("</COMPELATION>") 
 
     objWriter.Close() 
 

 
     'Writing XML content... 
 
     Dim xmldoc As XmlDocument = New XmlDocument() 
 
     xmldoc.Load(FILENAME) 
 

 
     With xmldoc.SelectSingleNode("/COMPELATION").CreateNavigator().AppendChild() 
 
      .WriteStartElement("DATA") 
 
      .WriteElementString("PROJECTID", "abc") 
 
      .WriteElementString("CLIENTID", "def") 
 
      .WriteElementString("CDATE", Date.Today) 
 
      .WriteElementString("CTIME", Now.ToShortTimeString) 
 

 
      .WriteEndElement() 
 
      .Close() 
 
     End With 
 
     xmldoc.Save(FILENAME) 
 

 
     Dim filepath As String = (FILENAME) 
 
     Dim DS_PROJECTS As New DataSet 
 
     DS_PROJECTS.ReadXml(filepath) 
 

 
     DataGridView1.DataSource = DS_PROJECTS.Tables(0) 
 
    End Sub 
 
End Class 
 
​

相關問題