XML : Excel To XML Not Saving All Column Header Elements

I've written a program that lets the user load an Excel file, preview the xml output that will be saved in a textbox, and save it as an XML file.

The problem is that column headers have null values in the cells below them are not writing to the XML file, and I need force them to be in the file as closed tags.

What's happening is if the column is empty and only has a title, it's not in the XML file at all. For example if the column header title is "Order Date" but there are no values in the column other than that title, I still need the XML to write <Order Date />.

So, my main question is this: How can I get the XML to write/output a closed attribute of a header column that has null values?

Other similar questions I found did not work for me are below:

Preserve empty Cells: With this one, I tried the suggestions of adding a ' character or a space under the column titles that do not have values, and this works to preserve the tags, but the format isn't what I need. The char ' makes the tag look like <Order Date>'</Order Date>, and the space makes the xml tag look like <Order Date xml:space="preserve"> </Order Date>, but like I mentioned above, the format must be <Order Date />.

Export DataTable to XML with ALL Columns: With this one, there's the mention of using AllowDBNull, but this is on a Datatable and I'm only using a Dataset, so I'm not sure if this is possible or how to go about it other than maybe writing a Dataset to a Datatable?

Second question, does anyone think it would be easier to hard-code XML elements within the code and then iterate through both the header values of the excel file to the column attributes of the hard-coded XML, and if they match up write the xml document that way?

My full code is below, and also I've only been coding in VB.net for about 3 weeks, and I'm sure I could be doing things more efficiently or easier, but I'm just not aware of them, so any help anyone could provide to put me in the right direction will be greatly appreciated it.

Full Code (Working, but doesn't output empty column headers as closed XML tags):

  Imports System.IO  Imports System.Data.OleDb    Public Class Form1      Dim myConnection As OleDbConnection      Dim myDS As DataSet, myAdapter As OleDbDataAdapter      'Load Excel File Button      Private Sub loadFileBtn_Click(sender As Object, e As EventArgs) Handles loadFileBtn.Click          'choose excel file          'file dialog box properties          OpenFileDialog1.Filter = "Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx"          OpenFileDialog1.FilterIndex = 2          OpenFileDialog1.InitialDirectory = "C:\"          saveXMLBtn.Enabled = False          If OpenFileDialog1.ShowDialog = DialogResult.OK Then              saveXMLBtn.Enabled = True          End If          'import file into datatable -> dataset -> xml          Dim myFilePath As String = OpenFileDialog1.FileName          Dim myExtension As String = Path.GetExtension(myFilePath)          Try              If myExtension = ".xls" Then                  myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + myFilePath + ";Extended Properties='Excel 8.0;'")              Else                  myConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myFilePath + ";Extended Properties='Excel 12.0;'")              End If              myAdapter = New OleDbDataAdapter("select * from [Sheet1$]", myConnection)              myDS = New DataSet              myAdapter.Fill(myDS)              myConnection.Close()              myAdapter.Dispose()              'dataset -> xml              'make null columns save then dataset -> xml              myDS.WriteXml(myFilePath, XmlWriteMode.IgnoreSchema)              'display xml in xmlpreviewbox              Dim storeXML As String = myDS.GetXml()              xmlPreviewBox.Text = storeXML          Catch ex As Exception              MsgBox(ex.ToString)          Finally              MsgBox("Preview of XML File Loaded.")          End Try      End Sub        'Save Converted Excel to XML File      Private Sub saveXMLBtn_Click(sender As Object, e As EventArgs) Handles saveXMLBtn.Click          'file dialog box properties          SaveFileDialog1.Filter = "XML Files (*.xml)|*.xml"          SaveFileDialog1.FilterIndex = 1          SaveFileDialog1.InitialDirectory = "C:\"          saveXMLBtn.Enabled = False          SaveFileDialog1.ShowDialog()          Try              If SaveFileDialog1.FileName <> "" Then                  myDS.WriteXml(SaveFileDialog1.FileName, XmlWriteMode.IgnoreSchema)              End If          Catch ex As Exception              MsgBox(ex.ToString)          Finally              MsgBox("XML File Saved Successfully.")          End Try        End Sub  End Class    

No comments:

Post a Comment