C# Serializing to XML Example Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kurious Oranj

    #1

    C# Serializing to XML Example Code

    This is the code that I currently have written to serialize the
    collection but it doesn't work when I put two "child" objects in. Anyone
    have any ideas what I'm doing wrong?

    ------------------------------------------------------------------------------------------

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Collecti ons;
    using System.Xml;
    using System.Xml.Seri alization;

    namespace TestXMLExport
    {
    [Serializable]
    public class Parent
    {
    public ChildCollection ChildCollection
    {
    set
    {
    childcollection = value;
    }
    get
    {
    return childcollection ;
    }
    }
    public Parent()
    {
    childcollection = new ChildCollection ();
    }
    private ChildCollection childcollection ;
    }
    [Serializable]
    public class Child
    {
    public string parAlpha
    {
    get
    {
    return paralpha;
    }
    set
    {
    paralpha = value;
    }
    }
    public string parBeta
    {
    get
    {
    return parbeta;
    }
    set
    {
    parbeta = value;
    }
    }
    private string parbeta;
    private string paralpha;
    }
    [Serializable]
    public class ChildCollection : CollectionBase
    {
    private ArrayList childcollection ;
    int position = -1;

    /// Main Constructor ///
    public ChildCollection ()
    {
    childcollection = new ArrayList();
    }
    public new int Count
    {
    get
    {
    return childcollection .Count;
    }
    set
    {
    }
    }

    public Child[] Childs
    {
    get
    {
    Child[] childs = new Child[childcollection .Count];
    childcollection .CopyTo(childs) ;
    return childs;
    }
    set
    {
    if (value == null) return;
    Child[] childs = (Child[])value;
    childcollection .Clear();
    foreach (Child child in childs)
    {
    childcollection .Add(child);
    }
    }
    }

    //IEnumerator and IEnumerable require these methods.
    public new IEnumerator GetEnumerator()
    {
    return (IEnumerator)th is;
    }

    //IEnumerator
    public bool MoveNext()
    {
    position++;
    return (position < childcollection .Count );
    }

    //IEnumerable
    public void Reset()
    {
    position = -1;
    }

    //IEnumerable
    public object Current
    {
    get
    {
    return childcollection[position];
    }
    }
    public void Add(Child Child)
    {
    childcollection .Add(Child);
    }

    public Child this[int index]
    {
    get
    {
    return (Child)childcol lection[index];
    }
    set
    {
    childcollection[index] = value;

    }
    }
    }
    }
Working...