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

How to convert C# class to XML ?

I'm building my first major C# WinForms application. I've successfully
completed the forms and the data model and they work great. Now I'm moving
on to saving/opening the data model to/from the hard drive.

In my VB6 past I've almost exclusively used MDB files for this purpose but
I'm thinking that XML would be a simpler, better solution for this
application. So I tried using "XMLSerialization" but it failed. I'm not
really sure why but am guessing because my data model doesn't comply with all
the restrictions it imposes.

So today I've been reading about standard XML formats. It all seems
straightforward and I'm ready to start coding. But I'm wondering if there is
a tool within or without of Visual Studio that will simply take my data model
(a complex class) and produce the XML data and/or an XML schema?

Any other advice or articles on this would be appreciated too.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
1 3310
For the most part, automatic XML serialization is a simple matter, provided
you follow the rules which are not the most well known. You can always
manually create an XML document based on the values of your class... but that
would be a little work, a little more work would be building your own
serialization system using reflection, but that would take a little more work.

I would suggest going over your class and verify that all properties you
want to serialize have the [XmlElement()] attribute with a good name, and
that all properties you do not want used have the [XmlIgnore()] attribute.

Another thing to watch for is complicated objects that cannot easily be
represented as a string... a raw object for instance tends not to be able to
be serialized. Other items such as TimeSpans cannot be automatically

Worst case, set all but one of your properties to [XmlIgnore()] and do some
testing, see if the class will serialize with it, if so, convert one of the
new [XmlIgnore()]’s back to an element and try again. Keep doing this until
you identify which property it is that is keeping you from easily serializing.

Once that’s all done, deserializing is just as easy, just make sure though
that all of your properties marked with [XmlElement()] have sets attached and
you have a default constructor for the class.

Brendan

"Robert W." wrote:
I'm building my first major C# WinForms application. I've successfully
completed the forms and the data model and they work great. Now I'm moving
on to saving/opening the data model to/from the hard drive.

In my VB6 past I've almost exclusively used MDB files for this purpose but
I'm thinking that XML would be a simpler, better solution for this
application. So I tried using "XMLSerialization" but it failed. I'm not
really sure why but am guessing because my data model doesn't comply with all
the restrictions it imposes.

So today I've been reading about standard XML formats. It all seems
straightforward and I'm ready to start coding. But I'm wondering if there is
a tool within or without of Visual Studio that will simply take my data model
(a complex class) and produce the XML data and/or an XML schema?

Any other advice or articles on this would be appreciated too.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2

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

Similar topics

4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
3
by: convert a Class Library to web service | last post by:
can i convert a Class Library to be share in the internet as a web service i what thet all my classes in the Class Library will be shared
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
3
by: Ed | last post by:
Hi, dear all, Now there is a issue to convert C++ into CLI/C++. But there is a issue block me. Say Class MN is multiple derived from class MM and NN. But the CLI only allow single class...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.