Click here to Skip to main content
15,886,806 members
Articles / Programming Languages / XML

Serialize/Deserialize any object to an XML file

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
28 May 2013CPOL1 min read 76.1K   3.2K   38  
Code to serialize/deserialize any object to an XML file.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.ComponentModel;
using XmlSerializer;

namespace XmlSerializer
{
    public class MyCollectionEditor : CollectionEditor
    {
        public static event PropertyValueChangedEventHandler PropertyValueChanged;
        public static bool AllowEndUserToEditEveryThing = false;
        PropertyDescriptor property;
        // Inherit the default constructor from the standard
        // Collection Editor...
        public MyCollectionEditor(Type type) : base(type) { }

        // Override this method in order to access the containing user controls
        // from the default Collection Editor form or to add new ones...
        protected override CollectionForm CreateCollectionForm()
        {
            // Getting the default layout of the Collection Editor...
            CollectionForm collectionForm = base.CreateCollectionForm();
            var canEndUserEdit = property.Attributes[typeof(ObjectSerializerAttribute)] as ObjectSerializerAttribute;
            if (!AllowEndUserToEditEveryThing&& canEndUserEdit != null && !canEndUserEdit.CanEndUserEditThis)
               DisableAddRemoveButtons(collectionForm);
            TableLayoutPanel tlpLayout = collectionForm.Controls[0] as TableLayoutPanel;
       
            if (tlpLayout != null)
            {
                // Get a reference to the inner PropertyGrid and hook
                // an event handler to it.
                if (tlpLayout.Controls[5] is PropertyGrid)
                {
                    PropertyGrid propertyGrid = tlpLayout.Controls[5] as PropertyGrid;
                    propertyGrid.HelpVisible = true;
                    propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(propertyGrid_PropertyValueChanged);
                }
            }

            return collectionForm;
        }
        
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
           property= context.PropertyDescriptor;
            return base.EditValue(context, provider, value);
        }
        private void DisableAddRemoveButtons(Control parent)
        {
            if (parent is Button && (parent.Text == "&Add" || parent.Text == "&Remove"))
                parent.Visible = false;
            foreach (Control item in parent.Controls)
                DisableAddRemoveButtons(item);
        }
        void propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
        {
            // Fire our customized collection event...
            if (PropertyValueChanged != null)
                PropertyValueChanged(sender, e);
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Desktop Team
Palestinian Territory (Occupied) Palestinian Territory (Occupied)
I have advanced skills in desktop apps development, i have built many apps for many corporations.

i'm using the follow languages in my work:
1- C# Language.
2- VB6 Language.
3- Asp.net using C#.
4- Crystal Reports.

i have more than 7 experience years in coding and programming.

Systems and apps Developed by Us:
1-E-Archive System (VB6,Supports multi DataBase Engins).
2-SMS System (Multi languages such VB6,C#,Asp.Net and Gizmox).
3-Multi Camera Monitor System-Motion Detection and record(C#).
4-Administrative Evaluation System (Asp.net).
5-E-Clinic System (C#-using my business layer generator,Supports multi DataBase Engines).
6-Data Access Layer Generator Framework(C#).
7-Sip Provider -VOIP- Softphone (C#- for Italian company).
8-Training course Manager System(C#-for UCAS).
9-Computer Exam System (VB6,Oracle DataBase).
10-Dialer System (VB6).
11-Implement Google API using C# (Translation and web Search).
12-Print Management Enterprise(C#)
13-Elections System (C#- using my business layer generator).
14-Advanced English competition (VB6).
15-Remote USB Sharing (C/C++,VB6).
16-Watch Attendance (C/C++,VB6).
17-Tube Spy (C#).
18-AdminYourTube (C#)
19- Transport Reservation System (C#,Asp.net,Ajax,Jquery)

Please visit my elance URL http://ashrafnet.elance.com

Comments and Discussions