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 12 4009
"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
"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
"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?
"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?
"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.
"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.
"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
"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
"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.
"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.
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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). ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |