Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

XML serialization of IList

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
Happs



Joined: 13 Jul 2011
Posts: 62
Location: Sydney, Australia

PostPosted: Wed Jul 13, 2011 8:06 am    Post subject: XML serialization of IList Reply with quote

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
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed Jul 13, 2011 9:14 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Happs



Joined: 13 Jul 2011
Posts: 62
Location: Sydney, Australia

PostPosted: Wed Jul 13, 2011 11:53 am    Post subject: Reply with quote

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
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu Jul 14, 2011 12:44 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Happs



Joined: 13 Jul 2011
Posts: 62
Location: Sydney, Australia

PostPosted: Thu Jul 14, 2011 2:45 am    Post subject: Reply with quote

Thank you, pleased with your customer support Smile.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group