HighTechTalks DotNet Forums  

Dataset.WriteXml how to force empty elements for DBNull Value in Columns ?

Dotnet XML microsoft.public.dotnet.xml


Discuss Dataset.WriteXml how to force empty elements for DBNull Value in Columns ? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Dataset.WriteXml how to force empty elements for DBNull Value in Columns ? - 04-06-2004 , 07:36 AM






I have a dataset that has DBNull in certain columns, now when I write
out this one to XML, I only get the columns as elements that do have
data in it. However I do need also the empty colums as empty elements
in the XML. How to do that ?
I don't understand why there is no simple option to specify the output
format, or did I miss something ?

regards
andreas

Reply With Quote
  #2  
Old   
Derek Harmon
 
Posts: n/a

Default Re: Dataset.WriteXml how to force empty elements for DBNull Value in Columns ? - 04-08-2004 , 09:06 PM






"Andreas Palm" <apa57 (AT) hotmail (DOT) com> wrote

Quote:
I have a dataset that has DBNull in certain columns, now when I write
out this one to XML, I only get the columns as elements that do have
data in it.
: :
I don't understand why there is no simple option to specify the output
format, or did I miss something ?
It's not formatting, but a data loss issue. The XML produced by
WriteXml( ) and consumed by ReadXml( ) is round-trip capable.
By "reformatting" the XML in this manner, you're actually losing
the information content of the original data set.

A relational data column that allows null values maps to an
optional XML element. When the value in that column is null,
the XML element does not occur. This makes sense because
null, by definition, is nothing. An empty element, on the other
hand, is something (the zero-length empty string, "").

If WriteXml( ) permitted this alteration, then ReadXml( ) loses
its ability to determine whether the empty element represents
DBNull or String.Empty when consuming the XML back into
the DataSet.

Quote:
However I do need also the empty colums as empty elements
in the XML. How to do that ?
If you insist, there is a way to get WriteXml( ) to produce empty
elements, although it's expensive in the sacrifices made.

As I've explained, if the column value is String.Empty, then
WriteXml( ) will emit an empty element. What's necessary
then is to map the null data value onto String.Empty. This
can be done with the DataColumn's DefaultValue property.

When the column's data value is DBNull, it will get replaced
by the DefaultValue of String.Empty.

One obvious caveat is the type-compatibility of the column's
DataType. What if it can't be converted to String.Empty, such
as might be the case for numeric columns or data types that
are simply rehashed numeric types (like enums)?

There is one data type in the CLR that can be used to envelope
all other data types, and that is convertible to a string through the
ToString( ) method. That's System.Object.

Another important note about DefaultValue is that it can only
replace null data values as they are loaded from a data source.
Therefore, both DefaultValues, and where necessary, DataType,
must be established prior to the data entering the DataSet.

Here's the breakdown:

Strings and DateTimes - if the DataColumn has either of these
types, then you can keep it and it'll work with a DefaultValue of
String.Empty.

Numerics, Enums, and Guids - if the DataColumn in the data
source has any of these types, then the DataType of the Data-
Column (best set in the constructor when initializing the Data-
Tables) must be System.Object to accept a DefaultValue of
String.Empty.

In summary,

1. Create the DataColumns, DataTables, DataRelations and
DataSet. Make sure the DataTypes you construct the Data-
Columns (of any allow-null columns which you want this behavior)
are one of these three types: String, DateTime or Object.

2. Set the DefaultValue of these DataColumns to String.Empty.

3. Load the data into the DataSet.

4. Export the data from the DataSet using WriteXml( ).


Derek Harmon




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.