[quoted text, click to view] "AFN" <DELETEnewsgroupCAPSaccount@yahoo.com> wrote in message
news:19rAc.4611$fd.133@twister.socal.rr.com...
> 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?
....
[quoted text, click to view] > Here's my code:
Here's mine:
Public Shared Function GetXML(ByVal dtDataTable As DataTable, ByVal
xwXmlTextWriter As XmlTextWriter) As String
If dtDataTable Is Nothing Then
Throw New Exception("DataTable cannot be nothing") ' Should
really use ApplicationException
End If 'End of If objDataTable Is Nothing Then
Dim intCounter As Int32
xwXmlTextWriter.WriteStartElement(dtDataTable.TableName)
Dim row As DataRow
For Each row In dtDataTable.Rows
xwXmlTextWriter.WriteStartElement("Row")
Dim col As DataColumn
For Each col In dtDataTable.Columns
xwXmlTextWriter.WriteElementString(col.ColumnName,
row(col).ToString())
Next
xwXmlTextWriter.WriteEndElement()
Next
xwXmlTextWriter.WriteEndElement()
Return xwXmlTextWriter.ToString()
End Function
Some comments:
1) If you're going to prefix variables with a type abbreviation, I recommend
against using "obj". Everything is an object, so that doesn't tell you
anything.
2) I generally use "For Each" loops instead of using an index unless I need
to use the index within the loop. The index is just an artifact of the fact
that you're looping.
3) You don't have to use ToString on strings. col.ColumnName is already a
string.
--
John Saunders
johnwsaundersiii at hotmail