C++ XML Serialization Program Example
|
C++ XML Serialization Program Example
Create a new CLR console application project and you can use the project and solution names as shown in the following Figure.
Add/edit the following classes, functions and the main() codes. |
// SimpleXmlSerializeCP.cpp : main project file.
#include "stdafx.h"
using namespace System; using namespace System::IO; using namespace System::Xml::Serialization;
public ref class Instrument { public: String^ Name; };
public ref class Brass: public Instrument { public: bool IsValved; };
public ref class Orchestra { public: array<Instrument^>^Instruments; };
void SerializeObject( String^ filename ) { // Each overridden field, property, or type requires an XmlAttributes object. Console::WriteLine("Instantiate XmlAttributes..."); XmlAttributes^ attrs = gcnew XmlAttributes;
// Create an XmlElementAttribute to override the // field that returns Instrument objects. The overridden field // returns Brass objects instead. Console::WriteLine("Overiding \"Instrument\", returning \"Brass\"..."); XmlElementAttribute^ attr = gcnew XmlElementAttribute; attr->ElementName = "Brass"; attr->Type = Brass::typeid;
// Add the element to the collection of elements. Console::WriteLine("Adding element..."); attrs->XmlElements->Add(attr);
// Create the XmlAttributeOverrides object. Console::WriteLine("Creating the XmlAttributeOverrides object..."); XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
// Add the type of the class that contains the overridden // member and the XmlAttributes to override it with to the // XmlAttributeOverrides object. Console::WriteLine("Adding the type of the class that contains the overridden..."); Console::WriteLine(" and XmlAttributes to override..."); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );
// Create the XmlSerializer using the XmlAttributeOverrides. Console::WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides..."); XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
// Writing the file requires a TextWriter. Console::WriteLine("Writing the file using TextWriter..."); TextWriter^ writer = gcnew StreamWriter( filename );
// Create the object that will be serialized. Console::WriteLine("Creating the \"Orchestra\" object that will be serialized..."); Orchestra^ band = gcnew Orchestra;
// Create an object of the derived type. Console::WriteLine("Creating an object of the derived type..."); Brass^ i = gcnew Brass; i->Name = "Trumpet"; i->IsValved = true; array<Instrument^>^myInstruments = {i}; band->Instruments = myInstruments; Console::WriteLine("The element\'s name: " + i->Name);
// Serialize the object. Console::WriteLine("Serializing the object..."); s->Serialize( writer, band ); Console::WriteLine("Object has been serialized..."); Console::WriteLine("Closing the TextWriter..."); writer->Close(); }
void DeserializeObject( String^ filename ) { Console::WriteLine("Instantiating the related objects..."); XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes;
// Create an XmlElementAttribute to override the Instrument. Console::WriteLine("Creating an XmlElementAttribute to override the \"Instrument\"..."); XmlElementAttribute^ attr = gcnew XmlElementAttribute; attr->ElementName = "Brass"; attr->Type = Brass::typeid; Console::WriteLine("The element\'s name: " + attr->ElementName);
// Add the element to the collection of elements. Console::WriteLine("Adding the element (Brass) to the collection of elements...."); attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );
// Create the XmlSerializer using the XmlAttributeOverrides. Console::WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides..."); XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Console::WriteLine("De-serializing the file object..."); Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs )); Console::WriteLine("\nBrass:" );
// The difference between deserializing the overridden // XML document and serializing it is this: To read the derived // object values, you must declare an object of the derived type // (Brass), and cast the Instrument instance to it. Brass^ b; System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator(); while ( myEnum->MoveNext() ) { Instrument^ i = safe_cast<Instrument^>(myEnum->Current); b = dynamic_cast<Brass^>(i); Console::WriteLine( "Name: {0}\nIsValved?: {1}", b->Name, b->IsValved ); } }
int main(array<System::String ^> ^args) { Console::WriteLine("Passing a file name for serialization..."); SerializeObject( "Override.xml" ); Console::WriteLine("\nPassing a file name for de-serialization and overriding..."); DeserializeObject( "Override.xml" ); return 0; } |
Build and run the project. The following is the sample output screenshot.
|
Create a new console application project and you can use the project and solution names as shown in the following Figure.
Add/edit the using directives as shown below.
using System; using System.IO; using System.Xml.Serialization; |
Add the following classes.
public class Orchestra { public Instrument[ ] Instruments; }
public class Instrument { public string Name; }
public class Brass : Instrument { public bool IsValved; } |
Change the Program class’s name to Run and add the public modifier for the Run class and the Main() method.
Add the Main() code.
public static void Main(string[ ] args) { Run test = new Run(); Console.WriteLine("Calling the SerializeObject method & passing a file name..."); test.SerializeObject("Override.xml"); Console.WriteLine(); Console.WriteLine("Calling the DeserializeObject method & passing a file name..."); test.DeserializeObject("Override.xml"); } |
Add the SerializeObject() and DeserializeObject() methods.
public void SerializeObject(string filename) { // Each overridden field, property, or type requires an XmlAttributes object Console.WriteLine("Instantiating the XmlAttributes object..."); XmlAttributes attrs = new XmlAttributes();
// Create an XmlElementAttribute to override the // field that returns Instrument objects. The overridden field returns Brass objects instead. Console.WriteLine("Creating an XmlElementAttribute to override the field that returns Instrument objects...."); Console.WriteLine(" The overridden field returns Brass objects instead..."); XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); Console.WriteLine("Element name is " + attr.ElementName + " with Brass type...");
// Add the element to the collection of elements. Console.WriteLine("Adding the Brass element to the collection of elements..."); attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object. Console.WriteLine("Creating the XmlAttributeOverrides object..."); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
// Add the type of the class that contains the overridden // member and the XmlAttributes to override it with to the XmlAttributeOverrides object. Console.WriteLine("Add the Orchestra type of the class that contains the overridden member..."); Console.WriteLine(" and the XmlAttributes to override it with to the XmlAttributeOverrides object..."); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides. Console.WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides..."); XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides);
// Writing the file requires a TextWriter. Console.WriteLine("Writing the file using a TextWriter..."); TextWriter writer = new StreamWriter(filename);
// Create the object that will be serialized. Console.WriteLine("Creating the Orchestra object that will be serialized..."); Orchestra band = new Orchestra();
// Create an object of the derived type. Console.WriteLine("Creating an object of the derived type..."); Brass i = new Brass(); i.Name = "Trumpet"; i.IsValved = true; Instrument[] myInstruments = { i }; band.Instruments = myInstruments;
// Serialize the object. Console.WriteLine("Serializing the object..."); s.Serialize(writer, band); Console.WriteLine("Object has been serialized..."); Console.WriteLine("Closing the StreamWriter..."); writer.Close(); }
public void DeserializeObject(string filename) { Console.WriteLine("Instantiating the related objects..."); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes();
// Create an XmlElementAttribute to override the Instrument. Console.WriteLine("Creating an XmlElementAttribute to override the Instrument..."); XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); Console.WriteLine("Element's name: " + attr.ElementName + " of type Brass");
// Add the element to the collection of elements. Console.WriteLine("Adding the element to the collection of elements..."); attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides. Console.WriteLine("Creating the XmlSerializer using the XmlAttributeOverrides..."); XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides);
Console.WriteLine("Creating a new file stream..."); FileStream fs = new FileStream(filename, FileMode.Open); Console.WriteLine("Deserializing the file object..."); Orchestra band = (Orchestra)s.Deserialize(fs); Console.WriteLine("Brass:");
// The difference between deserializing the overridden // XML document and serializing it is this: To read the derived // object values, you must declare an object of the derived type // (Brass), and cast the Instrument instance to it. Brass b; foreach (Instrument i in band.Instruments) { b = (Brass)i; Console.WriteLine("Name: " + b.Name + "\n" + "IsValved: " + b.IsValved); } } |
Build and run the project. The following is the sample output.