473,544 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dataset.WriteXm l 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 26612
"Andreas Palm" <ap***@hotmail. com> wrote in message news:d0******** *************** ***@posting.goo gle.com...
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 "reformatti ng" 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
jameswest
1 New Member
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
555
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 no way of knowing what the tables are, or what they contain. I am currently using the Dataset.WriteXML method passing an XMLWriter.
3
4570
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. I'm getting some strange results depending the input XML file I use. I was wondering if somebody could help me understand what is going on or point...
3
10275
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 using xmltextwriter to match schema without really using the xsd at the time of writing. The file I create validates but I was wondering is...
1
1995
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 there are no more records to be written to the XML file from the Dataset. Any ideas here, John.
4
6623
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 it tripled to about 1500kb for total traffic to send or receive these datasets thru the webservice. The files can be zipped down about 99% with...
9
2936
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 try to serialize/deserialize dataset schema, I get the error below during deserialization: "System.ArgumentException: The DefaultValue for column...
0
1493
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 columns as text) into a DataTable, fields that are an empty string ("") are set to DBNull in my data table. I need to serialize this dataset to an xml...
19
19295
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
2428
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 write method writes out post which are DBNull to the xml document. Then when I read the document using report.ReadXml(script, XmlReadMode.ReadSchema);
0
7437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7625
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7781
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5306
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3427
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.