473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Proper use of the IXmlSerializable Interface.

Airslash
221 100+
Hello,

I'm currently struggeling a bit with the Serialization aspects of the .NET framework. The Binary serialization is no problem, works relatively easy and is easy to implement. It's the XML serialization that's causing me problems at the moment.

I've been trying to sniff through the MSDN documentation regarding the IXmlSerializable itnerface, but the documentation is "lacking" at best...

So i've come up with the following implementation at the moment:

Expand|Select|Wrap|Line Numbers
  1. public System.Xml.Schema.XmlSchema GetSchema()
  2.         {
  3.             // Not used in the .NET environment anymoe.
  4.             return null;
  5.         }
  6.  
Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2.         /// This function is called by the XmlReader when the object is beeing 
  3.         /// deserialized from the Xml file. This function assigns all the values
  4.         /// back to the datamembers.
  5.         /// </summary>
  6.         /// <param name="reader">The XmlReader containing all the information.</param>
  7.         public void ReadXml(System.Xml.XmlReader reader)
  8.         {
  9.             // First consume the XML element, since we don't need this.
  10.             reader.ReadStartElement();
  11.  
  12.             // Open the root element of our class so we can retrieve the info.
  13.             reader.ReadStartElement("Database");
  14.  
  15.             // Read out each individual element using our private wrapper function.
  16.             m_command = ReadElement<IDbCommand>("Command", reader);
  17.             m_connection = ReadElement<IDbConnection>("Connection", reader);
  18.             m_transport = ReadElement<Transport>("Transport", reader);
  19.             m_port = ReadElement<uint>("Port", reader);
  20.             m_server = ReadElement<string>("Server", reader);
  21.             m_ip = ReadElement<string>("IpAddress", reader);
  22.             m_name = ReadElement<string>("Name", reader);
  23.             m_compression = ReadElement<uint>("Compression", reader);
  24.             m_blockreadsize = ReadElement<int>("BlockReadSize", reader);
  25.             m_username = ReadElement<string>("Username", reader);
  26.             m_password = ReadElement<string>("Password", reader);
  27.  
  28.             // Close the root element for our database.
  29.             reader.ReadEndElement();
  30.  
  31.             // Close the final xml element.
  32.             reader.ReadEndElement();
  33.         }
  34.  
Expand|Select|Wrap|Line Numbers
  1. public void WriteXml(System.Xml.XmlWriter writer)
  2.         {
  3.             // Write out all our internal datamembers.
  4.             // We do not have to write the root xml element, that's for the caller.
  5.             // So first we write the root element from our class.
  6.             writer.WriteStartElement("Database");
  7.  
  8.             // Since we don't have to write any attributes we can now write out
  9.             // each element individually.
  10.             WriteElement("Command", m_command, writer);
  11.             WriteElement("Connection", m_connection, writer);
  12.             WriteElement("Transport", m_transport, writer);
  13.             WriteElement("Port", m_port, writer);
  14.             WriteElement("Server", m_server, writer);
  15.             WriteElement("IpAddress", m_ip, writer);
  16.             WriteElement("Name", m_name, writer);
  17.             WriteElement("Compression", m_compression, writer);
  18.             WriteElement("BlockReadSize", m_blockreadsize, writer);
  19.             WriteElement("Username", m_username, writer);
  20.             WriteElement("Password", m_password, writer);
  21.  
  22.             // Write the closing tag of our class.
  23.             writer.WriteEndElement();
  24.         }
  25.  
and two helper function to make the serialization a bit easier for me.
Expand|Select|Wrap|Line Numbers
  1.         /// <summary>
  2.         /// Reads the specified element from the XmLReader and automaticly
  3.         /// casts it back to the specified type.
  4.         /// </summary>
  5.         /// <typeparam name="T">The type to be returned.</typeparam>
  6.         /// <param name="name">The name of the element to be read.</param>
  7.         /// <param name="reader">The reader to read the element from.</param>
  8.         /// <returns>The value from the reader cast to type T</returns>
  9.         private T ReadElement<T>(string name, System.Xml.XmlReader reader)
  10.         {
  11.             reader.ReadStartElement(name);                      // Open the element.
  12.             return (T)reader.ReadContentAs(typeof(T), null);    // Read and cast the value.
  13.             reader.ReadEndElement();                            // Close the element.
  14.         }
  15.  
  16.         /// <summary>
  17.         /// Writes the specified element in the XmlWriter and automaticly creates
  18.         /// the openening and closing tag for the element.
  19.         /// This function writes the specified element without attributes.
  20.         /// </summary>
  21.         /// <param name="name">The name of the Element to write</param>
  22.         /// <param name="value">The value of the Element</param>
  23.         /// <param name="writer">The write to write the element to.</param>
  24.         private void WriteElement(string name, object value, System.Xml.XmlWriter writer)
  25.         {
  26.             writer.WriteStartElement(name);
  27.             writer.WriteValue(value);
  28.             writer.WriteEndElement();
  29.         }
  30.  
The class is called database and has a bunch of private members that I want serialized. I'm not entirely sure if this is the way to go, but the documentation around this is bad.....
I know it can be done automaticly by just adding the attributes in the class such as [XmlRoot], but I wanted to work with the Interface...
Apr 30 '10 #1
0 1497

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Leepe | last post by:
example: I have a skill object that implements the XmlSerializable interface Implementing the getschema gives me some unwanted generations towards the wsdl.exe Reference.cs generation. skill is...
1
by: mattoc | last post by:
Happy new year to all. I have a strange error that I've been trying for a while now to fathom.. Basically I have a hierarchy of state classes that I need to serialize to XML. Some of them can...
0
by: Thomas D. | last post by:
Situation: --------------------------- I have an 'export'-wrapper to my regular objects. For each regular object there is also an export object. An export object derives from the regular object...
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
1
by: theburnetts | last post by:
I have a complex class heirarchy where all of my classes inherit from a single base class (BaseObject) and this BaseObject class inherits from XPBaseObject. XPBaseObject is a 3rd party base class...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
3
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary...
0
by: Jamie | last post by:
I've implemented 3 classes, all of which make use of the IXmlSerializable interface. Class A has 2 properties that are instances to class B and C. Class A - serialization routine: public...
2
by: sachy | last post by:
Hello All, I want to deserialize part of xml into a object. For this i am having class that implements the IXmlSerializable interface. For this class i get the xmlnode object that has the xml. ...
2
by: =?Utf-8?B?Ym9iYnk=?= | last post by:
I whave a custom membershipProvider which inherits from MembershiopUser also I want to implement IXmlSerializable. Is my following code correct. public class SSOMembershipUser :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.