473,396 Members | 1,996 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,396 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 ?

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

4
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...
3
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. ...
3
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...
1
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...
4
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...
9
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...
0
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...
19
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
2
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...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.