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: -
public System.Xml.Schema.XmlSchema GetSchema()
-
{
-
// Not used in the .NET environment anymoe.
-
return null;
-
}
-
-
/// <summary>
-
/// This function is called by the XmlReader when the object is beeing
-
/// deserialized from the Xml file. This function assigns all the values
-
/// back to the datamembers.
-
/// </summary>
-
/// <param name="reader">The XmlReader containing all the information.</param>
-
public void ReadXml(System.Xml.XmlReader reader)
-
{
-
// First consume the XML element, since we don't need this.
-
reader.ReadStartElement();
-
-
// Open the root element of our class so we can retrieve the info.
-
reader.ReadStartElement("Database");
-
-
// Read out each individual element using our private wrapper function.
-
m_command = ReadElement<IDbCommand>("Command", reader);
-
m_connection = ReadElement<IDbConnection>("Connection", reader);
-
m_transport = ReadElement<Transport>("Transport", reader);
-
m_port = ReadElement<uint>("Port", reader);
-
m_server = ReadElement<string>("Server", reader);
-
m_ip = ReadElement<string>("IpAddress", reader);
-
m_name = ReadElement<string>("Name", reader);
-
m_compression = ReadElement<uint>("Compression", reader);
-
m_blockreadsize = ReadElement<int>("BlockReadSize", reader);
-
m_username = ReadElement<string>("Username", reader);
-
m_password = ReadElement<string>("Password", reader);
-
-
// Close the root element for our database.
-
reader.ReadEndElement();
-
-
// Close the final xml element.
-
reader.ReadEndElement();
-
}
-
-
public void WriteXml(System.Xml.XmlWriter writer)
-
{
-
// Write out all our internal datamembers.
-
// We do not have to write the root xml element, that's for the caller.
-
// So first we write the root element from our class.
-
writer.WriteStartElement("Database");
-
-
// Since we don't have to write any attributes we can now write out
-
// each element individually.
-
WriteElement("Command", m_command, writer);
-
WriteElement("Connection", m_connection, writer);
-
WriteElement("Transport", m_transport, writer);
-
WriteElement("Port", m_port, writer);
-
WriteElement("Server", m_server, writer);
-
WriteElement("IpAddress", m_ip, writer);
-
WriteElement("Name", m_name, writer);
-
WriteElement("Compression", m_compression, writer);
-
WriteElement("BlockReadSize", m_blockreadsize, writer);
-
WriteElement("Username", m_username, writer);
-
WriteElement("Password", m_password, writer);
-
-
// Write the closing tag of our class.
-
writer.WriteEndElement();
-
}
-
and two helper function to make the serialization a bit easier for me. -
/// <summary>
-
/// Reads the specified element from the XmLReader and automaticly
-
/// casts it back to the specified type.
-
/// </summary>
-
/// <typeparam name="T">The type to be returned.</typeparam>
-
/// <param name="name">The name of the element to be read.</param>
-
/// <param name="reader">The reader to read the element from.</param>
-
/// <returns>The value from the reader cast to type T</returns>
-
private T ReadElement<T>(string name, System.Xml.XmlReader reader)
-
{
-
reader.ReadStartElement(name); // Open the element.
-
return (T)reader.ReadContentAs(typeof(T), null); // Read and cast the value.
-
reader.ReadEndElement(); // Close the element.
-
}
-
-
/// <summary>
-
/// Writes the specified element in the XmlWriter and automaticly creates
-
/// the openening and closing tag for the element.
-
/// This function writes the specified element without attributes.
-
/// </summary>
-
/// <param name="name">The name of the Element to write</param>
-
/// <param name="value">The value of the Element</param>
-
/// <param name="writer">The write to write the element to.</param>
-
private void WriteElement(string name, object value, System.Xml.XmlWriter writer)
-
{
-
writer.WriteStartElement(name);
-
writer.WriteValue(value);
-
writer.WriteEndElement();
-
}
-
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...
0 1497 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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 :...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |