View previous topic :: View next topic |
Author |
Message |
Happs
Joined: 13 Jul 2011 Posts: 62 Location: Sydney, Australia
|
Posted: Wed Jul 13, 2011 8:06 am Post subject: XML serialization of IList |
|
|
Hi,
I would like to XML serialize the object used in data source. Its known issue that XmlNode collection don't support the IList interface. I had a look at the forum and found only this post: http://www.infralution.com/phpBB2/viewtopic.php?t=1384&highlight=ilist+serialization
Do you recommend a best approach?
For example, how to serialize following VirtualTree.Samples.ProgrammaticBinding.Part class.
Code: |
[Serializable()]
public class Part
{
private Part _parentPart;
private ArrayList _childParts = new ArrayList();
private string _name;
public void Add(Part parent, string name)
{
_parentPart = parent;
_name = name;
if (_parentPart != null)
{
parent._childParts.Add(this);
}
}
public Part ParentPart
{
get { return _parentPart; }
set { _parentPart = value; }
}
public IList ChildParts
{
get { return _childParts; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
|
I have these two example methods to serialize and deserialize:
Code: |
/// <summary>
/// Serializes to XML.
/// </summary>
private void SerializeToXML(Part part)
{
using (Stream stream = new FileStream(@"C:\temp\part.xml", FileMode.Create, FileAccess.Write))
{
XmlSerializer serializer = new XmlSerializer(part.GetType());
serializer.Serialize(stream, part);
}
}
/// <summary>
/// Deserialize from XML.
/// </summary>
private Part DeserializeFromXML()
{
XmlSerializer deserializer = new XmlSerializer(typeof(Part));
TextReader textReader = new StreamReader(@"C:\temp\part.xml");
Part part = (Part)deserializer.Deserialize(textReader);
textReader.Close();
return part;
}
|
Thanks in advance. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Wed Jul 13, 2011 9:14 am Post subject: |
|
|
See the this code project article for a description of how to use the standard .NET serialization mechanism.
Personally I prefer to take control of reading/writing objects to XML myself and bypass some of the reflection magic in the standard .NET serialization. To do this I just define a ReadXml and WriteXml method on each object - something like:
Code: |
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("MyClass");
writer.WriteElementString("SomeProperty", SomeProperty);
writer.WriteEndElement();
}
public void ReadXml(XmlReader reader)
{
reader.ReadStartElement("MyClass");
while (reader.IsStartElement())
{
switch (reader.Name)
{
case "SomeProperty":
SomeProperty = reader.ReadElementString("SomeProperty");
break;
default:
// skip elements we don't recognise
// generally you probably want to also report them in some way
reader.Skip();
break;
}
}
reader.ReadEndElement();
} |
_________________ Infralution Support |
|
Back to top |
|
|
Happs
Joined: 13 Jul 2011 Posts: 62 Location: Sydney, Australia
|
Posted: Wed Jul 13, 2011 11:53 am Post subject: |
|
|
Thanks for your prompt response.
Thought to give you some background. We have an object model similar to your ProgrammaticBinding.Part class, where it can have child parts. We would like to XML serialize the object.
Thanks for the code project article, according to the article:
- Added a ChildPartsArray property then XML elements so it can be serialized.
- 'XmlIgnoreAttribute' to the ChildParts (Ilist) property as XmlSerialization don't support the IList interface.
- I had to add XmlIgnoreAttribute to the ParentPart property, because XML serialization does not support circular references. I was getting this error:
Quote: | A circular reference was detected while serializing an object of type Infralution.Controls.VirtualTree.Samples.ProgrammaticBinding.Part. |
Following code now seems to work and it serialize:
Code: |
/// <summary>
/// XML serialization does not support circular references. We need to ignore parent to serialize.
/// </summary>
[XmlIgnoreAttribute()]
public Part ParentPart
{
get { return _parentPart; }
set { _parentPart = value; }
}
/// <summary>
/// A list of the child parts
/// </summary>
[XmlIgnoreAttribute()]
public IList ChildParts
{
get { return _childParts; }
}
// Serializes an ArrayList as a "Parts" array of XML elements of type string named "Part".
[XmlArray("Parts"), XmlArrayItem("PartItem", typeof(Part))]
public ArrayList ChildPartsArray
{
get
{
return _childParts;
}
set
{
_childParts = value;
}
}
|
Do you have any comments to the above approach? Any side effect to the way virtual tree perform sort, etc. I would like to prove the concept first.
Thanks again for your help. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Thu Jul 14, 2011 12:44 am Post subject: |
|
|
If this serialization technique works for you then it should be OK. It doesn't really have any impact on the way Virtual Tree binds to the data. _________________ Infralution Support |
|
Back to top |
|
|
Happs
Joined: 13 Jul 2011 Posts: 62 Location: Sydney, Australia
|
Posted: Thu Jul 14, 2011 2:45 am Post subject: |
|
|
Thank you, pleased with your customer support . |
|
Back to top |
|
|
|