473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Null elements don't appear in XMLTextWriter results

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.Ta bleName)

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.C olumnName).ToString())
Next
objXmlTextWriter.WriteEndElement()
Next

objXmlTextWriter.WriteEndElement()

Return objXmlTextWriter.ToString

End Function
Nov 12 '05 #1
12 4008
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?
....
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.Tabl eName)

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
Nov 12 '05 #2
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?
....
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.Tabl eName)

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
Nov 12 '05 #3
AFN

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?
...
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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the function
and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?
Nov 12 '05 #4
AFN

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?
...
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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the function
and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?
Nov 12 '05 #5
AFN

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?


...
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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the function
and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But does
anyone know why the built-in framework GetXML method does NOT show empty
elements?

Also, can anyone still comment on how I would empty the buffer in my code?
I don't understand that but I read that you should do that. I don't know
where in the code and how.
Nov 12 '05 #6
AFN

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?


...
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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the function
and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But does
anyone know why the built-in framework GetXML method does NOT show empty
elements?

Also, can anyone still comment on how I would empty the buffer in my code?
I don't understand that but I read that you should do that. I don't know
where in the code and how.
Nov 12 '05 #7
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?

...

> 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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the

function and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But does
anyone know why the built-in framework GetXML method does NOT show empty
elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it means
not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code?
I don't understand that but I read that you should do that. I don't know
where in the code and how.


I don't see that you should "empty the buffer". The XmlWriter.ToString() at
the end will return the string representation of all the XML elements you've
written to it.
--
John Saunders
johnwsaundersiii at hotmail
Nov 12 '05 #8
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:19***************@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?

...

> 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.Tabl eName)

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


I appreciate your code suggestions. Thanks. I did not write the

function and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But does
anyone know why the built-in framework GetXML method does NOT show empty
elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it means
not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code?
I don't understand that but I read that you should do that. I don't know
where in the code and how.


I don't see that you should "empty the buffer". The XmlWriter.ToString() at
the end will return the string representation of all the XML elements you've
written to it.
--
John Saunders
johnwsaundersiii at hotmail
Nov 12 '05 #9
AFN

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
> "AFN" <DE************************@yahoo.com> wrote in message
> news:19***************@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?
>
> ...
>
> > 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.Tabl eName)
>
> 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
>
>

I appreciate your code suggestions. Thanks. I did not write the

function and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But

does anyone know why the built-in framework GetXML method does NOT show empty
elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it

means not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code? I don't understand that but I read that you should do that. I don't know where in the code and how.
I don't see that you should "empty the buffer". The XmlWriter.ToString()

at the end will return the string representation of all the XML elements you've written to it.
--
John Saunders
johnwsaundersiii at hotmail


It should show empty elements, IMO, to maintain the structure of the XML, so
that importing programs know that the field exists with a NULL value.
Otherwise, if field5 had Nulls in every record, in my example above, there
would be no <field5> reference anywhere in the XML and no importing program
would know of that field name.

Thanks for your reply again.
Nov 12 '05 #10
AFN

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
> "AFN" <DE************************@yahoo.com> wrote in message
> news:19***************@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?
>
> ...
>
> > 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.Tabl eName)
>
> 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
>
>

I appreciate your code suggestions. Thanks. I did not write the

function and you are absolutely correct.

BUT, it still doesn't solve my #1 problem of a missing <field5 /> when
field5 is null. Do you know how can I fix this?


I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But

does anyone know why the built-in framework GetXML method does NOT show empty
elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it

means not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code? I don't understand that but I read that you should do that. I don't know where in the code and how.
I don't see that you should "empty the buffer". The XmlWriter.ToString()

at the end will return the string representation of all the XML elements you've written to it.
--
John Saunders
johnwsaundersiii at hotmail


It should show empty elements, IMO, to maintain the structure of the XML, so
that importing programs know that the field exists with a NULL value.
Otherwise, if field5 had Nulls in every record, in my example above, there
would be no <field5> reference anywhere in the XML and no importing program
would know of that field name.

Thanks for your reply again.
Nov 12 '05 #11
"AFN" <DE************************@yahoo.com> wrote in message
news:Tz****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...
>
> "John Saunders" <jo**************@notcoldmail.com> wrote in message
> news:um**************@TK2MSFTNGP11.phx.gbl...
> > "AFN" <DE************************@yahoo.com> wrote in message
> > news:19***************@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?
> >
> > ...
> >
> > > 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.Tabl eName)
> >
> > 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
> >
> >
>
>
>
> I appreciate your code suggestions. Thanks. I did not write the

function
> and you are absolutely correct.
>
> BUT, it still doesn't solve my #1 problem of a missing <field5 />
when > field5 is null. Do you know how can I fix this?
>
>

I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But

does anyone know why the built-in framework GetXML method does NOT show empty elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it

means
not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code? I don't understand that but I read that you should do that. I don't know where in the code and how.


I don't see that you should "empty the buffer". The XmlWriter.ToString()

at
the end will return the string representation of all the XML elements

you've
written to it.
--
John Saunders
johnwsaundersiii at hotmail


It should show empty elements, IMO, to maintain the structure of the XML,

so that importing programs know that the field exists with a NULL value.
Otherwise, if field5 had Nulls in every record, in my example above, there
would be no <field5> reference anywhere in the XML and no importing program would know of that field name.

Thanks for your reply again.


In that case, there should be two parts to the XML. One should be the
schema, which would indicate all of the fields possible in the data. The
other would be the data itself, which would have no entries for NULL fields.
--
John Saunders
johnwsaundersiii at hotmail
Nov 12 '05 #12
"AFN" <DE************************@yahoo.com> wrote in message
news:Tz****************@twister.socal.rr.com...

"John Saunders" <jo**************@notcoldmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"AFN" <DE************************@yahoo.com> wrote in message
news:iH****************@twister.socal.rr.com...

"AFN" <DE************************@yahoo.com> wrote in message
news:ve****************@twister.socal.rr.com...
>
> "John Saunders" <jo**************@notcoldmail.com> wrote in message
> news:um**************@TK2MSFTNGP11.phx.gbl...
> > "AFN" <DE************************@yahoo.com> wrote in message
> > news:19***************@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?
> >
> > ...
> >
> > > 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.Tabl eName)
> >
> > 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
> >
> >
>
>
>
> I appreciate your code suggestions. Thanks. I did not write the

function
> and you are absolutely correct.
>
> BUT, it still doesn't solve my #1 problem of a missing <field5 />
when > field5 is null. Do you know how can I fix this?
>
>

I just realized that the previous programmer was calling the .net
framework's DataSet.GetXML() method instead of this custom function I
posted. Duh!!! My function does correctly show <field5 />. But

does anyone know why the built-in framework GetXML method does NOT show empty elements?


Why should it show empty elements? NULL in SQL doesn't mean empty, it

means
not present or "I don't know".
Also, can anyone still comment on how I would empty the buffer in my code? I don't understand that but I read that you should do that. I don't know where in the code and how.


I don't see that you should "empty the buffer". The XmlWriter.ToString()

at
the end will return the string representation of all the XML elements

you've
written to it.
--
John Saunders
johnwsaundersiii at hotmail


It should show empty elements, IMO, to maintain the structure of the XML,

so that importing programs know that the field exists with a NULL value.
Otherwise, if field5 had Nulls in every record, in my example above, there
would be no <field5> reference anywhere in the XML and no importing program would know of that field name.

Thanks for your reply again.


In that case, there should be two parts to the XML. One should be the
schema, which would indicate all of the fields possible in the data. The
other would be the data itself, which would have no entries for NULL fields.
--
John Saunders
johnwsaundersiii at hotmail
Nov 12 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T Conti | last post by:
Howdy: I am currently writing a .Net handler to replace an ASP page. The ASP module used a COM dll to format XML. This dll was pretty robust and it auto-suppressed empty elements and...
0
by: AFN | last post by:
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: ...
3
by: Clark Spencer | last post by:
I have built a small integration app using VS .NET 2003 that extracts orderinformation from a 'webshop'. Extracting the orderinformation works fine. Appending the order elements in the...
1
by: Kenny Mullican | last post by:
I am using complex types in order to support serialization/deserialization of floating point numbers, since floating points can't be null. I've seen how to suppress attributes that are "not...
2
by: Oenone | last post by:
I am using an XmlTextWriter to create an XML document, which we then send to a client. The document is failing to validate against the DTD we are providng when the load it into their...
4
by: Steven.Dahlin | last post by:
My C# Winform WebService client is attempting to access data from a Web Service written in Java and running on an Oracle App Server 10.3. I have a TCP/IP monitor running and have verified that...
14
by: Rob Cowie | last post by:
I'm having a bit of trouble with this so any help would be gratefully recieved... After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag...
1
by: SAM DLG | last post by:
I'm not sure how to do this, but in my n-tiered app, i need to pass xmlreaders as parameters as opposed to strings and as opposed to xmldocuments(). here's what I have, I can write fine to a...
7
by: Jason | last post by:
I have some XML that contains some null elements, for example: <Document> <StartDate/> <EndDate/> </Document> I would like to process the XML using the .NET serialization tools (XSD.exe). ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.