Quantcast
Viewing all articles
Browse latest Browse all 12583

How to serialize MDI Child Form Properties to XML using XMLSerialization?

Good Day, I'd like to ask as to how will I be able to get the property values of my Mdi Child forms. I'm actually having a difficulty to retrieve the property values of my MDI Child Forms since I'm new to XML. I used this following codes to attempt to access the MDIChild Form properties and Iterate through them and store them in a List for me to be able to serialize those list and place them inside the XML file. 

Here is my MDI Parent Form syntax:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Reflection;
namespace ReadWriteXmlMdi
{
    public partial class MDIParentForm : Form
    {
        CreateChildForms childFrms = new CreateChildForms();
        ReadWriteClass RWClass = new ReadWriteClass();
        public MDIParentForm()
        {
            InitializeComponent();
        }

        private void NewChildFrm_Click(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childFrms.CreateChild(this, out childForm);
            childForm.Show();
            
        }

        private void MDIParentForm_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Hello!");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            RWClass.WriteToXML(childFrms.ListOfChildFrms);
            MessageBox.Show("Bye!");
        }
    }
}

While this is my codes used to create child forms and write in to the XML file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.Dynamic;
using System.Collections;

namespace ReadWriteXmlMdi
{
    public class CreateChildForms
    {
        public int childFormNumber = 0;
        public List<object> ListOfChildFrms = new List<object>();
        public void CreateChild(MDIParentForm x, out Form childFrm)
        {
            ChildForm child = new ChildForm();
            child.MdiParent = x;
            child.Text = "Window " + ++childFormNumber;
            childFrm = child;
            ListOfChildFrms.Add(child);
        }
    }

    public class ReadWriteClass
    {
        public void WriteToXML(List<object> ChildForms)
        {
            SaveFileDialog saveXml = new SaveFileDialog();
            saveXml.Filter = "XML Files | *.xml";
            saveXml.Title = "Where To Save XML File";
            if (saveXml.ShowDialog() == DialogResult.OK)
            {
                string XmlPath = saveXml.FileName;
                //Type[] extraTypes = new Type[1];
                //extraTypes[0] = typeof(CreateChildForms);
                XmlSerializer serializer = new XmlSerializer(ChildForms.GetType());
                FileStream fs = new FileStream(XmlPath, FileMode.OpenOrCreate, FileAccess.Write);
                serializer.Serialize(fs, ChildForms);
                fs.Close();
            }
        }
    }
}
I've tried to use a Type array for the serializer to be able to know which type to serialize however it causes an error regarding reflection. The requirements actually is to be able to store the properties of my child forms into an XML file whenever I close the MDI parent and whenever I open the MDI parent form again, all the MDI child forms will be loaded as they were after I closed the MDI parent. I do hope you can help me resolve my error or suggest a much simpler way on how to access properties of a control or a form and place it in an XML file since I'm kinda new to XML serialization. Thank you.



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>