C Serialize To Xml

C Serialize To Xml 3,9/5 8474 votes
  1. C++ Boost Serialization Examples
  2. C Sharp Serialize Object To Xml
  3. C++ Serialize Object
  4. C Sharp Serialize Object To Xml String

There are many situations where we need to convert class objects to XML and XML to class objects in real-time projects. For the demonstration I have two classes named Employee and Address. Serialization is the process of converting an object into a stream of bytes. In this article, I will show you how to serialize object to XML in C#. XML serialization converts the public fields and properties of an object into an XML stream. Mar 02, 2008  As to which serialization framework to use, I highly recommend Boost's serialization library. It has very good versioning support, allows binary, text, or XML archives, and in simple cases allows you to use the same function as both the serializer and deserializer. Serialization is the process of converting an object into a stream of bytes. In this article, I will show you how to serialize object to XML in C#. XML serialization converts the public fields and properties of an object into an XML stream. In consequence XML deserialization does not work and your application may crash on start. If the file is not huge, I suggest first serialize object to MemoryStream then write the stream to the File. This case is especially important if there is some complicated custom serialization. The article talks about serialization of objects in XML format and deserialization of an XML file back to an object. Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization on the other hand is used to convert the byte of data.

Active15 days ago

How do I Deserialize this XML document:

I have this:

.

.

that don't seem to work :-(

Dariusz Woźniak
6,0803 gold badges41 silver badges62 bronze badges
AlexAlex
13.6k21 gold badges41 silver badges41 bronze badges

17 Answers

Here's a working version. I changed the XmlElementAttribute labels to XmlElement because in the xml the StockNumber, Make and Model values are elements, not attributes. Also I removed the reader.ReadToEnd(); (that function reads the whole stream and returns a string, so the Deserialze() function couldn't use the reader anymore..the position was at the end of the stream). I also took a few liberties with the naming :).

Here are the classes:

The Deserialize function:

And the slightly tweaked xml (I needed to add a new element to wrap <Cars>..Net is picky about deserializing arrays):

Kevin TigheKevin Tighe
13.3k3 gold badges28 silver badges36 bronze badges

How about you just save the xml to a file, and use xsd to generate C# classes?

  1. Write the file to disk (I named it foo.xml)
  2. Generate the xsd: xsd foo.xml
  3. Generate the C#: xsd foo.xsd /classes

Et voila - and C# code file that should be able to read the data via XmlSerializer:

(include the generated foo.cs in the project)

DaveInCaz
4,4364 gold badges23 silver badges46 bronze badges
Marc GravellMarc Gravell
825k213 gold badges2228 silver badges2624 bronze badges

You have two possibilities.

Method 1. XSD tool

Suppose that you have your XML file in this location C:pathtoxmlfile.xml
  1. Open Developer Command Prompt
    You can find it in Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools Or if you have Windows 8 can just start typing Developer Command Prompt in Start screen
  2. Change location to your XML file directory by typing cd /D 'C:pathtoxml'
  3. Create XSD file from your xml file by typing xsd file.xml
  4. Create C# classes by typing xsd /c file.xsd

And that's it! You have generated C# classes from xml file in C:pathtoxmlfile.cs

Method 2 - Paste special

Required Visual Studio 2012+
  1. Copy content of your XML file to clipboard
  2. Add to your solution new, empty class file (Shift+Alt+C)
  3. Open that file and in menu click Edit > Paste special > Paste XML As Classes

And that's it!

Usage

C++ Boost Serialization Examples

Usage is very simple with this helper class:

All you have to do now, is:

Damian DrygielDamian Drygiel
16.2k4 gold badges28 silver badges27 bronze badges

The following snippet should do the trick (and you can ignore most of the serialization attributes):

..

erymski

See if this helps:

.

And failing that use the xsd.exe program that comes with visual studio to create a schema document based on that xml file, and then use it again to create a class based on the schema document.

Bodhi puja. Version History Here bldhi can find the changelog of Bodhi Pooja Gatha since it was posted on our website on A Tea Party Fit for godhi Queen.

Joel CoehoornJoel Coehoorn
322k100 gold badges510 silver badges744 bronze badges

I don't think .net is 'picky about deserializing arrays'. The first xml document is not well formed.There is no root element, although it looks like there is. The canonical xml document has a root and at least 1 element (if at all). In your example:

janbakjanbak

try this block of code if your .xml file has been generated somewhere in disk and if you have used List<T>:

Note: C:serialize.xml is my .xml file's path. You can change it for your needs.

sheetal nainwalsheetal nainwal

Kevin's anser is good, aside from the fact, that in the real world, you are often not able to alter the original XML to suit your needs.

There's a simple solution for the original XML, too:

And then you can simply call:

Kim HomannKim Homann

Try this Generic Class For Xml Serialization & Deserialization.

Hasan JavaidHasan Javaid

I found the answers here to be very helpful, that said I still struggled (just a bit) to get this working. So, in case it helps someone I'll spell out the working solution:

XML from Original Question. The xml is in a file Class1.xml, a path to this file is used in the code to locate this xml file.

Macklemore and ryan lewis split. I used the answer by @erymski to get this working, so created a file called Car.cs and added the following:

C Sharp Serialize Object To Xml

The other bit of code provided by @erymski ..

.. goes into your main program (Program.cs), in static CarCollection XCar() like this:

Hope it helps :-)

SlowLearnerSlowLearner

The idea is to have all level being handled for deserializationPlease see a sample solution that solved my similar issue

The above XML is handled in two level

The Inner level

Same Way you need multiple level with car as arrayCheck this example for multilevel deserialization

Community
makdumakdu
5471 gold badge8 silver badges18 bronze badges

If you're getting errors using xsd.exe to create your xsd file, then use the XmlSchemaInference class as mentioned on msdn. Here's a unit test to demonstrate:

goku_da_mastergoku_da_master

You can just change one attribute for you Cars car property from XmlArrayItem to XmlElment. That is, from

to

XU WeijiangXU Weijiang

My solution:

  1. Use Edit > Past Special > Paste XML As Classes to get the class in your code
  2. Try something like this: create a list of that class (List<class1>), then use the XmlSerializer to serialize that list to a xml file.
  3. Now you just replace the body of that file with your data and try to deserialize it.

C++ Serialize Object

Code:

NOTE: you must pay attention to the root name, don't change it. Mine is 'ArrayOfClass1'

haiduong87haiduong87

How about a generic class to deserialize an XML document

This part may, or may not be necessary. Open the XML document in Visual Studio, right click on the XML, choose properties. Then choose your schema file.

David C FuchsDavid C Fuchs
ZoidbergseasharpZoidbergseasharp
Andre MAndre M

C Sharp Serialize Object To Xml String

Not the answer you're looking for? Browse other questions tagged c#asp.netxmlserializationxml-deserialization or ask your own question.