C Serialize Xml To Object

  1. C# Serialize To Xml File
  2. C Sharp Serialize Object To Xml String
  1. Jul 11, 2017  The Serialize method is used to serialize an object to XML. Serialize is overloaded and can send output to a TextWriter, Stream, or XMLWriter object.
  2. I was looking for a lightweight and easy to use XML Serialization library for C. Mainly for simple tasks like to store application settings or to send messages. After looking around I found a few candidates. But most of them are too complicated or to heavy. I thought there must be a simple.

Serialization / Deserialization; Member-Classes; Member-Collections; Copy / Clone objects; Compare objects; Find / Replace values; Complete code in 1 header and 1 code file (+tinyxml2) Using the code. Let's have a look at the example 'application settings': The first step is to define your class containing all needed setting values.

13 Feb 2014CPOL
Simple and lightweight XML Serialization using tinyxml2

Introduction

Serialize

I was looking for a lightweight and easy to use XML Serialization library for C++.
Mainly for simple tasks like to store application settings or to send messages.
After looking around I found a few candidates. But most of them are too complicated or to heavy.
I thought there must be a simple solution, without defining a bulk of interfaces or helper classes.

How it works

C Serialize Xml To Object

Creating serializable classes is pretty simple and straightforward.

  1. Derive you class from 'Serializeable'
  2. Add members
    Members can be basic types like xString, xInt, xBool
    or complex types like member classes or collections of member classes.
  3. Register members in the constructor

Features

C# serialize object to string
  • Serialization / Deserialization
  • Member-Classes
  • Member-Collections
  • Copy / Clone objects
  • Compare objects
  • Find / Replace values
  • Complete code in 1 header and 1 code file (+tinyxml2)

Using the code

Let's have a look at the example 'application settings':
The first step is to define your class containing all needed setting values.
As you can see, serializeable members are defined as xString, xInt, xBool...
All other types are allowed, of course. They are simply ignored during the serialization process.

The second step is to register all x-members for serialization.
This is usually done in the constructor:

Set and access member values in your code:

Serialize the class to string:

XML - Result:

Deserialize from string:

Member Classes and Collections

Additionally we want to store database login and a list of used document items:

The updated ApplicationSettings class definition including document collection and login data.

Using member classes and collections:

Final XML:

Tools

Compare objects
Clone/Copy objects
Find/Replace

C# Serialize To Xml File


Other frameworks

If you need a complete XML framework, have a look at Brian Aberle's XMLFoundation.
Nice Article!

History

Version 1.0: Initial publication.

C Sharp Serialize Object To Xml String