Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Xml-Serializing a derived class using IXmlSerializable

I've got a base class which is compatible with XML serialization and a derived class which implements IXmlSerializable.

In this example, the base class does implement IXmlSerializable:


using System.Diagnostics;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerializationDerived
{
    public class Foo
    {
        public int fooProp;

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            fooProp = int.Parse (reader.ReadElementString ("fooProp"));
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString ("fooProp", fooProp.ToString ());
        }
    }

    public class Bar : Foo, IXmlSerializable
    {
        public new void ReadXml(XmlReader reader)
        {
            base.ReadXml (reader);
        }

        public new void WriteXml(XmlWriter writer)
        {
            base.WriteXml (writer);
        }

        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder ();
            XmlWriter writer = XmlWriter.Create (sb);

            Bar bar = new Bar ();
            bar.fooProp = 42;

            XmlSerializer serializer = new XmlSerializer (typeof (Bar));
            serializer.Serialize (writer, bar);

            Debug.WriteLine (sb.ToString ());
        }
    }
}

This produces this output:

<?xml version="1.0" encoding="utf-16"?><Bar><fooProp>42</fooProp></Bar>

However, i'd like to use a base class which does not implement IXmlSerializable. This prevents using base.Read/WriteXml. The result will be:

<?xml version="1.0" encoding="utf-16"?><Bar />

Is there any way to still get the desired result?

like image 701
mafu Avatar asked Feb 09 '09 12:02

mafu


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Improving mtlung's answer, why don't you use XmlSerializer? You can tune up your class with attribute so it can be serialized the way you want, and it's pretty simple to do.

using System.Xml.Serialization;

...

[XmlRoot("someclass")]
public class SomeClass
{
    [XmlAttribute("p01")]
    public int MyProperty01
    {
        get { ... }
    }

    [XmlArray("sometypes")]
    public SomeType[] MyProperty02
    {
        get { ... }
    }

    [XmlText]
    public int MyProperty03
    {
        get { ... }
    }

    public SomeClass()
    {
    }
}

Then, serializing and deserializing it would be pretty simple:

void Save(SomeClass obj)
{
    XmlSerializer xs = new XmlSerializer(typeof(SomeClass));
    using (FileStream fs = new FileStream("c:\\test.xml", ...))
    {
        xs.Serialize(fs, obj);
    }
}

void Load(out SomeClass obj)
{
    XmlSerializer xs = new XmlSerializer(typeof(SomeClass));
    using (FileStream fs = new FileStream("c:\\test.xml", ...))
    {
        obj = xs.Deserialize(fs);
    }
}

And the resulting XML would be something like this:

<someclass p01="...">
  <sometype>
    <!-- SomeType serialized objects as child elements -->
  </sometype>
  # value of "MyProperty03" as text #
</someclass>

This method works better with "POCO" classes, and it's simple and clean. You don't even have to use the attributes, they are there to help you customize the serialization.

like image 156
Ricardo Nolde Avatar answered Oct 05 '22 13:10

Ricardo Nolde