473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "XMLSerializati on" 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 3332
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 "XMLSerializati on" 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
3632
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
6
10218
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
6
1407
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 declaration statments (first lines) e.g. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String Private Function ConvertHundreds(ByVal MyNumber As String) As String etc.
3
3560
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
10088
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 it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
5
3789
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 List<RoleData> GetRoles() { return GetRoles(null, false); }
17
71445
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
1591
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 derived. My understanding is to create a new interface INN with same method declaration with class NN. And let class MN derived from MM and INN, then copy the implementation
9
17557
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
10772
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8944
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
9445
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
9306
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
9234
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
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
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
6030
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
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

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.