Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dataset to XML - How to include "Null"/blank columns 1

Status
Not open for further replies.

onedunpark

IS-IT--Management
Jan 17, 2003
19
GB
Hi,

I have a dataset where some of the values are "". When writing the dataset to XML using the following approach, it only writes those columns that have values. I really need all columns to be written to the file regardless of whether or not a value exists.

Dim objAdapter As SqlClient.SqlDataAdapter = New
SqlClient.SqlDataAdapter(SQL, GetDBProvider(DBName))

Dim objDataTable As New System.Data.DataSet
objAdapter.Fill(objDataTable)
objAdapter.Dispose() : objAdapter = Nothing
objDataTable.WriteXml(XMLPath, XmlWriteMode.IgnoreSchema)

I'm sure this is really easy but I'm stumped and having looked at code all over the place am starting to confuse myself. I'm guessing I have to iterate through the dataset and extract/write each value in turn, but looking at the other code examples I'm at a bit of loss as to how

Any and all replies appreciated

Steven
 
I guess about the simplest way would be to do something like:
Code:
  Private Function DataTableToXML(ByVal table As DataTable) As XmlDocument
    Dim xml As New XmlDocument
    xml.LoadXml("<tableData/>")
    For Each dr As DataRow In table.Rows
      Dim rowElem As XmlElement
      rowElem = xml.CreateElement("row")
      For Each col As DataColumn In table.Columns
        Dim colElem As XmlElement
        colElem = xml.CreateElement(col.ColumnName)
        colElem.InnerText = dr(col).ToString()
        rowElem.AppendChild(colElem)
      Next
      xml.DocumentElement.AppendChild(rowElem)
    Next dr
    Return xml
  End Function
And call with:
Code:
Dim xml As XmlDocument
    xml = DataTableToXML(MyDataSet.Tables(0))

Jon

"I don't regret this, but I both rue and lament it.
 
JontyMC,

Thanks for the reply. Greatly appreciated.

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top