473,224 Members | 1,489 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,224 software developers and data experts.

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

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 ?

Nov 12 '05 #1
2 26516
"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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
by: Joe98765 | last post by:
I have data coming from SQL Server and need to write out some XML. I have a schema(xsd) for this XML file. Right now I am bringing in the DataSet and writing out the elements and attributes myself...
by: John | last post by:
Hi All, When I delete the last record from my dataset and then WriteXML() to the file; the Table itself, in the xml file, is removed. I need to prevent the table structure from being deleted if...
by: Dan | last post by:
Hi, I pass datasets back and forth between client app and web services (hosted on iis 5). When written to xml file these datasets can be 500kb+. From a bandwitdth meter it seems this amount...
by: PeterWellington | last post by:
I have a column in a data table that stores enum values and assigns a default value: Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek)) dc.DefaultValue = DayOfWeek.Thursday When I...
by: kevin | last post by:
using vs 2003 the csv file has every field value enclosed in double quotes: i.e. no null values Whe using OleDbDataAdapter to read a csv file (I do create the schema.ini file defining all...
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
by: leonards | last post by:
Hello I have the opposite problem as http://www.thescripts.com/forum/thread176792.html When I'm writng XML files with report.WriteXml(scriptName +".xml",XmlWriteMode.WriteSchema); The...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.