By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Andreas Palm" <ap***@hotmail.com> wrote in message news:d0**************************@posting.google.c om...
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.
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
Nov 12 '05 #2

P: 1
I have a problem similar to this. In our app, if you open a customer record, there are options to export the customer data to an xml file and to import customer data from an xml file. Business rules dictate that when an xml file is imported, the data it creates must be identical to the source data used to create the xml file. If the original field contained NULL then the field in the new record must also be filled with NULL. If I am understanding this solution correctly, if the source data set contains NULL the xml file will create an element with an empty string. If that file is imported, I am thinking that the empty string elements will load not NULL, but an empty string. Am I right about that? Is there ANY way to create an xml document from a data set so that the null data set values will create NULL elements? (using nil=true syntax)
Jul 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.