473,698 Members | 2,281 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 3327
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
3628
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
10204
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
3553
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
10081
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
3785
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
71429
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
1590
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
17544
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
10766
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
8676
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
8608
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
9029
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
8897
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
8867
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...
0
7732
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
6522
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
5860
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
4370
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...

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.