How to Override an Attribute With XML

Utilizing the "XmlAttributeOverrides" class and its methods is helpful when you want to override an attribute while serializing a "XmlSerializer" object. This lets you serialize objects in multiple ways from a single set of serializable classes. It's also possible to extend upon a serializable class from a DLL that you don't have access to. A practical application of this is to serialize members of a class instance as XML attributes instead of XML elements.

Instructions

    • 1

      Open your code's source file in an editor such as Microsoft Visual Studio Express.

    • 2

      Add the "System," "System::IO" and "System::Xml::Serialization" libraries at the top of your file. This lets you access the XML "serializer" functions and objects. In C++ this is done by adding the code:

      using namespace System;

      using namespace System::IO;

      using namespace System::Xml::Serialization;

    • 3

      Define three public classes to use with the XML functions. In C++ this is done by adding the following code at the top of your file before your functions:

      public ref class Fruit

      {

      public:

      String^ ripe_fruit;

      };

      public ref class Apple: public Fruit

      {

      public:

      int amount_apples;

      };

      public ref class Basket

      {

      public:

      array<Fruit^>^Fruits;

      };

    • 4

      Create new "XmlAttributes" and "XmlElementAttribute" objects in your function, initialize the "XmlElementAttribute" object to use your replacement class and then add the new element to the collection of elements. In C++ this is done by adding the code:

      XmlAttributes^ my_attrs = gcnew XmlAttributes;

      XmlElementAttribute^ my_attr = gcnew XmlElementAttribute;

      my_attr->ElementName = "Apple";

      my_attr->Type = Apple::typeid;

      my_attrs->XmlElements->Add(my_attr);

    • 5

      Create a new "XmlAttributeOverrides" object and call its "Add" method to add the overriding class's type and the "XmlAttributes" object to override it with. In C++ this is done by adding the code:

      XmlAttributeOverrides^ my_attr_override = gcnew XmlAttributeOverrides;

      my_attr_override->Add( Basket::typeid, "Fruits", attrs );

    • 6

      Create a new "XmlSerializer" object and initialize it with the "XmlAttributeOverrides" object. In C++ this is done by adding the code:

      XmlSerializer^ my_serializer = gcnew XmlSerializer(Basket::typeid, my_attr_override);

    • 7

      Create an object to serialize. In C++ this is done by adding the code:

      Basket^ my_basket = gcnew Basket

    • 8

      Create a new object using the derived class. In C++ this is done by adding the code:

      Apple^ a = gcnew Apple;

      a->ripe_fruit = "Slightly Ripe";

      a->amount_apples = 2;

      array<Fruit^>^ my_Fruits = {a};

      my_basket->Fruits = my_Fruits;

    • 9

      Create a "TextWriter" object and then serialize your object. In C++ this is done by adding the code:

      TextWriter^ tw = gcnew StreamWriter("file.txt");

      my_serializer->Serialize(tw, my_basket);

      tw->Close();

      The code has overridden the "Fruit" class, allowing the field to accept the "Apple" class, which it inherits from the "Fruit" class.

    • 10

      Save the file, compile and run the program to override the attributes.

Related Searches:

References

Comments

Related Ads

Featured