A
AFN
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:
<field5></field5> or <field5 />
but instead it doesn't even show the field at all for those records where
field5 is Null! Instead it just shows:
<field4>Whatever</field4>
<field6>Whatever</field6>
This concerns me because if people take XML file output and import into a
database, they won't even know field5 exists unless some of the records have
a non null value. But sometimes field5 is null in all records. So how do
I get the writer to make an empty <field5 /> representation so database
imports won't get screwed up? On a side note, where would I flush the
buffer in my code below?
Here's my code:
Public Shared Function GetXML(ByVal objDataTable As DataTable, ByVal
objXmlTextWriter As XmlTextWriter) As String
If IsNothing(objDataTable) Then
Throw New Exception("DataTable cannot be nothing")
End If 'End of If Not Nothing(objDataTable) Then
Dim intCounter As Int32
objXmlTextWriter.WriteStartElement(objDataTable.TableName)
For intCounter = 0 To objDataTable.Rows.Count - 1
objXmlTextWriter.WriteStartElement("Row")
Dim objDataColumn As DataColumn
For Each objDataColumn In objDataTable.Columns
objXmlTextWriter.WriteElementString(objDataColumn.ColumnName.ToString(),
objDataTable.Rows(intCounter).Item(objDataColumn.ColumnName).ToString())
Next
objXmlTextWriter.WriteEndElement()
Next
objXmlTextWriter.WriteEndElement()
Return objXmlTextWriter.ToString
End Function
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:
<field5></field5> or <field5 />
but instead it doesn't even show the field at all for those records where
field5 is Null! Instead it just shows:
<field4>Whatever</field4>
<field6>Whatever</field6>
This concerns me because if people take XML file output and import into a
database, they won't even know field5 exists unless some of the records have
a non null value. But sometimes field5 is null in all records. So how do
I get the writer to make an empty <field5 /> representation so database
imports won't get screwed up? On a side note, where would I flush the
buffer in my code below?
Here's my code:
Public Shared Function GetXML(ByVal objDataTable As DataTable, ByVal
objXmlTextWriter As XmlTextWriter) As String
If IsNothing(objDataTable) Then
Throw New Exception("DataTable cannot be nothing")
End If 'End of If Not Nothing(objDataTable) Then
Dim intCounter As Int32
objXmlTextWriter.WriteStartElement(objDataTable.TableName)
For intCounter = 0 To objDataTable.Rows.Count - 1
objXmlTextWriter.WriteStartElement("Row")
Dim objDataColumn As DataColumn
For Each objDataColumn In objDataTable.Columns
objXmlTextWriter.WriteElementString(objDataColumn.ColumnName.ToString(),
objDataTable.Rows(intCounter).Item(objDataColumn.ColumnName).ToString())
Next
objXmlTextWriter.WriteEndElement()
Next
objXmlTextWriter.WriteEndElement()
Return objXmlTextWriter.ToString
End Function