473,770 Members | 6,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data serialization options

I've been learning about mechanisms .NET offers to export data. The
initial goal is to see what sorts of ways are available to save an
application's state (document, internal database, whatever). Not counting
storing data in a database (which is obviously suitable for some things,
but not necessary or even necessarily desirable for other things), here's
what I've explored:

Serializable attribute, with BinaryFormatter and SoapFormatter
Implement IXmlSerializabl e

The methods based on Serializable are in fact fairly easy, but they don't
produce output that would be readily usable in other applications. What I
like about it is that with some pretty simple and brief code, one can
easily serialize whatever data in the class one wants (and for some very
basic classes, you don't even have to write code). I only had to write a
half-dozen lines of code specific to the serialization, and that only
because my top-level data structure is a generic collection, which isn't
supported implicitly.

It was no surprise to me that the output using BinaryFormatter isn't
readily importable into other applications, but I had higher hopes for
SoapFormatter. Probably because I know so little about SOAP, but I was
hoping that with it being based on XML, it would generate an XML document
that I could load into, for example, Excel and have it readily understand
it (the data I'm dealing here is essentially a list of items, and I want
to be able to read the list into Excel with one item per row, each field
in its own column).

Instead, the SoapFormatter creates all these links within the document,
with sections corresponding to each data type, among other things. Excel
can, of course, read the document but because it doesn't have a
traditional tree-based layout, it doesn't really import the data in a
useful way.

Since I wanted the XML document to be organized more like the original
list the data is in, I looked at implementing IXmlSerializabl e. Not that
this was all that hard either, but I did find that I had to write a _lot_
more code, because there's nothing in the XML serializing stuff that
automatically deals with non-public fields in a class. I don't want
writable properties on my classes and the XmlSerializer.S erialize() docs
say that it will only serialize public members.

As it was, I had to make what are really internal classes public, because
the auto-generated assemblies need access to the types (as near as I can
tell, this is solely because of the need to construct the relevant
object...seems like if they'd added some sort of static object
construction method to IXmlSerializabl e, this need could be avoided in a
lot of cases, but they didn't so it's not :( ).

So, now I have a bunch of code, which is what looks to me to probably be
about 90% of what I'd have to write if I just did all the XML
serialization explicitly, without even using the XmlSerializer class. And
it required that I expose parts of my assembly that really don't need to
be exposed, by making the classes public.

I'm curious as to whether there are other methods of saving data that I've
overlooked. I noticed that there's WAY more stuff in the
Xml.Serializati on namespace than I've actually used. I've barely
scratched the surface. But the docs seem pretty explicit about the fact
that it won't automatically handle non-public members, so I don't really
know what all that other stuff might do for me.

Am I really best off just doing this all explicitly and forgetting the
built-in serialization classes? Because of the more general-purpose XML
support, doing that isn't really all that hard, but I'm curious if that's
really the best way to go.

Thanks for any advice,
Pete
Jun 20 '07 #1
2 3846
Peter,

Have you looked at the DataContractSer ializer in .NET 3.0? It is what
WCF uses by default to serialize data on the wire. It supports
Serializable, IXmlSerializabl e, and a new serialization model called the
DataContract. You apply DataContract to your class, and then DataMember to
your fields/properties (it is opt-in) and it will serialize those members.

It produces XML as well, and it is much cleaner than what the
SoapFormatter creates (mostly because type information is not embedded in
the xml) so it should be more what you are looking for.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
I've been learning about mechanisms .NET offers to export data. The
initial goal is to see what sorts of ways are available to save an
application's state (document, internal database, whatever). Not counting
storing data in a database (which is obviously suitable for some things,
but not necessary or even necessarily desirable for other things), here's
what I've explored:

Serializable attribute, with BinaryFormatter and SoapFormatter
Implement IXmlSerializabl e

The methods based on Serializable are in fact fairly easy, but they don't
produce output that would be readily usable in other applications. What I
like about it is that with some pretty simple and brief code, one can
easily serialize whatever data in the class one wants (and for some very
basic classes, you don't even have to write code). I only had to write a
half-dozen lines of code specific to the serialization, and that only
because my top-level data structure is a generic collection, which isn't
supported implicitly.

It was no surprise to me that the output using BinaryFormatter isn't
readily importable into other applications, but I had higher hopes for
SoapFormatter. Probably because I know so little about SOAP, but I was
hoping that with it being based on XML, it would generate an XML document
that I could load into, for example, Excel and have it readily understand
it (the data I'm dealing here is essentially a list of items, and I want
to be able to read the list into Excel with one item per row, each field
in its own column).

Instead, the SoapFormatter creates all these links within the document,
with sections corresponding to each data type, among other things. Excel
can, of course, read the document but because it doesn't have a
traditional tree-based layout, it doesn't really import the data in a
useful way.

Since I wanted the XML document to be organized more like the original
list the data is in, I looked at implementing IXmlSerializabl e. Not that
this was all that hard either, but I did find that I had to write a _lot_
more code, because there's nothing in the XML serializing stuff that
automatically deals with non-public fields in a class. I don't want
writable properties on my classes and the XmlSerializer.S erialize() docs
say that it will only serialize public members.

As it was, I had to make what are really internal classes public, because
the auto-generated assemblies need access to the types (as near as I can
tell, this is solely because of the need to construct the relevant
object...seems like if they'd added some sort of static object
construction method to IXmlSerializabl e, this need could be avoided in a
lot of cases, but they didn't so it's not :( ).

So, now I have a bunch of code, which is what looks to me to probably be
about 90% of what I'd have to write if I just did all the XML
serialization explicitly, without even using the XmlSerializer class. And
it required that I expose parts of my assembly that really don't need to
be exposed, by making the classes public.

I'm curious as to whether there are other methods of saving data that I've
overlooked. I noticed that there's WAY more stuff in the
Xml.Serializati on namespace than I've actually used. I've barely
scratched the surface. But the docs seem pretty explicit about the fact
that it won't automatically handle non-public members, so I don't really
know what all that other stuff might do for me.

Am I really best off just doing this all explicitly and forgetting the
built-in serialization classes? Because of the more general-purpose XML
support, doing that isn't really all that hard, but I'm curious if that's
really the best way to go.

Thanks for any advice,
Pete
Jun 20 '07 #2
On Tue, 19 Jun 2007 17:49:05 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard .caspershouse.c omwrote:
Have you looked at the DataContractSer ializer in .NET 3.0?
Nope. Haven't even installed .NET 3.0 yet. :)
[...]
It produces XML as well, and it is much cleaner than what the
SoapFormatter creates (mostly because type information is not embedded
in the xml) so it should be more what you are looking for.
Nice...thanks! I will take a look.

Pete
Jun 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2953
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been serialized. In my program I am calling a webservice which then return the data to the calling program. The program then have to serialize the data and create an XML-document.
2
2508
by: Robert Magnusson | last post by:
Hi all, I have a healthy class defined that happily serializes and deserializes from the underlying XML file. The problem I hit is that, as soon as I add an implicit conversion in any of the classes (I'm converting from my class into OleDbParameter), the deserialization fails. The following is the error displayed when the code fails during ASPX (C#) processing:
7
11930
by: BS | last post by:
Hello everybody I'm calling a webservice that returns complex data. The goal is to populate a datagrid with it. Using a loop for each record found ( such as For i = 0 To oResponse.historicalData.Length - 1 ) no problem to load a datagrid. However, when I try to bind directly the datasource to the web service
2
2484
by: Angelos Karantzalis | last post by:
Hi y'all, we're using a bunch of classes in a project, that we wanted to convert to-from xml easily. So, we defined XmlAttribute annotations for our class members and all worked fine. The Xml stream came from an external source with a custom xml schema, so we had no alternative. However, along the way we came to the point that we must expose those classes as return types for a web service. Returning an instance of a class through the...
4
1814
by: radiax | last post by:
Iam trying to find a simple solution for sharing data between windows applications( apart of using file system or remoting or MMF) . I tried using class library by making data members "shared" but it seems that data cant not be passed between applications. what am I doing wrong here? what are "simple" possible solutions? thanks
8
9213
by: Richard Collette | last post by:
When attempting to debug a webservice, I get the error: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary. In reading about web services (http://msdn2.microsoft.com/en-us/library/ds492xtk.aspx), the documentation states that exceptions will automatically be handled as a fault element in the message and then converted to a SoapException in the client.
0
2056
by: nicomp | last post by:
I created a Web Service: I imported System.Data.SqlClient so I could access SQL server tables programmatically. The web service builds and deploys with no problems. When I try to add the corresponding Web Reference to the Web Site project I get the error listed below. I am able to create other Web Services on the same server and I am able to add Web Referneces to them. I have narrowed it down to the "Imports System.Data.SqlClient"...
0
2788
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the fields to map to an xsd. Then i call a web service upload method that wants the xml strongly typed to the xsd in a string. so I have generated a class from a xsd newcust.cs in .net 1.1 c# to start with just to see what it means to use an...
11
3823
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it doesn't seem to work. The code below is what I'm trying to use. Anyone see any obvious errors? or have any hints/pointers? regards, Igor float floatRead = 0;
0
9591
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.