XmlSerializer for class based on Interface

G

Guest

Hello ,
I have a class which I serialize using XMLSerializer.
This class has public properties which are based on other interfaces.
Because of this I am unable to serialize the object.
Error : There was an error reflecting the type XXXX
Is there any way to serialize this object w/o moving away from Interface
based classes.
Many Thanks!
Eg.
//=========Interfaces===============
public interface IActor
{
[XmlElement("name")]
string Name { get; set; }

[XmlElement("agent")]
IAgent Agent { get; set; }
}

public interface IAgent
{
[XmlElement("rate")]
double Rate { get; set; }
}


//=========Classes===============
[XmlRoot("actor")]
public class Actor : IActor
{
private string name;

[XmlElement("name")]
public string Name
{
get { return name; }
set { name = value; }
}

private IAgent agent;

[XmlElement("broker")]
public IAgent Agent
{
get { return agent; }
set { agent = value; }
}

//other customer methods
}



public class Agent : IAgent
{
private double rate;

[XmlElement("rate")]
public double Rate
{
get { return rate; }
set { rate = value; }
}

}

//============Test===============
[STAThread]
public static void Main()
{
XmlSerializer s;
TextWriter w;

#region actor

IActor actor = new Actor();
actor.Name = "Bob";
IAgent agent = new Agent();
agent.Rate = 12.34D;
actor.Agent = agent;

// Serialization
s = new XmlSerializer(typeof (Actor));
w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, actor);
w.Close();

#endregion actor
Console.ReadLine();
}
 
G

Guest

I had to add an additional constructor in additon to the default constructor
as a default is required for serialization.

That did not work. I now get an error : There was an error reflecting type
'Actor'


Ciaran O''Donnell said:
Give the constructor the typeof(IAgent) too.

Ciaran O'Donnell

Tantr Mantr said:
Hello ,
I have a class which I serialize using XMLSerializer.
This class has public properties which are based on other interfaces.
Because of this I am unable to serialize the object.
Error : There was an error reflecting the type XXXX
Is there any way to serialize this object w/o moving away from Interface
based classes.
Many Thanks!
Eg.
//=========Interfaces===============
public interface IActor
{
[XmlElement("name")]
string Name { get; set; }

[XmlElement("agent")]
IAgent Agent { get; set; }
}

public interface IAgent
{
[XmlElement("rate")]
double Rate { get; set; }
}


//=========Classes===============
[XmlRoot("actor")]
public class Actor : IActor
{
private string name;

[XmlElement("name")]
public string Name
{
get { return name; }
set { name = value; }
}

private IAgent agent;

[XmlElement("broker")]
public IAgent Agent
{
get { return agent; }
set { agent = value; }
}

//other customer methods
}



public class Agent : IAgent
{
private double rate;

[XmlElement("rate")]
public double Rate
{
get { return rate; }
set { rate = value; }
}

}

//============Test===============
[STAThread]
public static void Main()
{
XmlSerializer s;
TextWriter w;

#region actor

IActor actor = new Actor();
actor.Name = "Bob";
IAgent agent = new Agent();
agent.Rate = 12.34D;
actor.Agent = agent;

// Serialization
s = new XmlSerializer(typeof (Actor));
w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, actor);
w.Close();

#endregion actor
Console.ReadLine();
}
 
G

Guest

I ran into a similar problem as well. Unforutnately, the default .Net
serializer is not smart enough to pick out the runtime instance of the
interface type. You will have to implement your own serialization routine by
implementing IXmlSerizanble in your class.

A complete listing of your code with IXmlSerializable implemented is given
below:

--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace TestConsoleApplication
{
//=========Interfaces===============
public interface IActor : IXmlSerializable
{
[XmlElement("name")]
string Name { get; set; }

[XmlElement("agent")]
IAgent Agent { get; set; }
}

public interface IAgent : IXmlSerializable
{
[XmlElement("rate")]
double Rate { get; set; }
}


//=========Classes===============
[XmlRoot("actor")]
public class Actor : IActor, IXmlSerializable
{
public Actor()
{}

private string name;

[XmlElement("name")]
public string Name
{
get { return name; }
set { name = value; }
}

private IAgent agent;

[XmlElement("broker")]
public IAgent Agent
{
get { return agent; }
set { agent = value; }
}

//other customer methods

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
name = reader["Name"];

reader.Read(); // Skip ahead to next node

if (reader.MoveToContent() == XmlNodeType.Element &&
reader.LocalName == "broker")
{
agent = new Agent();
agent.ReadXml(reader);
}
}

public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("Name", Name);

writer.WriteStartElement("broker");
agent.WriteXml(writer);
writer.WriteEndElement();

}

#endregion
}



public class Agent : IAgent, IXmlSerializable
{
private double rate;

[XmlElement("rate")]
public double Rate
{
get { return rate; }
set { rate = value; }
}

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
rate = double.Parse(reader["rate"]);
}

public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("rate", rate.ToString());
}

#endregion
}

class test
{
//============Test===============
[STAThread]
public static void Main()
{
XmlSerializer s;
TextWriter w;

#region actor

IActor actor = new Actor();
actor.Name = "Bob";
IAgent agent = new Agent();
agent.Rate = 12.34D;
actor.Agent = agent;

// Serialization
s = new XmlSerializer(typeof(Actor));
w = new StreamWriter(@"c:\list.xml");
s.Serialize(w, actor);
w.Close();

// deserialize
XmlSerializer xs = new XmlSerializer(typeof(Actor));
FileStream fs = new FileStream(@"c:\\list.xml", FileMode.Open);
fs.Position = 0;
IActor actor2 = (Actor)xs.Deserialize(fs);
Console.WriteLine(string.Format("Actor Name = {0} Broker.Rate =
{1}", actor2.Name, actor2.Agent.Rate));

#endregion actor
Console.ReadLine();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top