简体   繁体   中英

Serialize C# form to XML

I have created a customer information form and bound the text boxes to properties in the form class. This simple form has 10 text boxes on the form that are bound to properties in the form

  • Customer name
  • PhonePrimary.phoneNumber
  • PhonePrimary.phoneType
  • PhonePrimary.TextMessageOK
  • PhoneDaytime.phoneNumber
  • PhoneDaytime.phoneType
  • PhoneDaytime.TextMessageOK
  • PhoneEvening.phoneNumber
  • PhoneEvening.phoneType
  • PhoneEvening.TextMessageOK

Once these values have been filled out, I would like to create an XML file to save these values so that they can be retrieved at a later time.

using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace SimpleCustomerInfo
{
    public partial class CustomerInfoForm : Form
    {
        CustomerInfo ci;

        public CustomerInfoForm()
        {
            InitializeComponent();
            ci = new CustomerInfo();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(CustomerInfoForm));
            TextWriter textWriter = new StreamWriter(@"C:\testme.xml");
            serializer.Serialize(textWriter, ci);
            textWriter.Close();
        }

        public partial class CustomerInfo
        {
            public string CustomerName { get; set; }
            public PhoneInfo PhonePrimary { get; set; }
            public PhoneInfo PhoneDays { get; set; }
            public PhoneInfo PhoneEvening { get; set; }
        }

        public class PhoneInfo
        {
            public string number { get; set; }
            public string type { get; set; }
            public bool textOk { get; set; }
        }

    }


}

When the form is filled in and the save button is pressed and error is generated. The most inner exception error is: {"Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface."} Data: {System.Collections.ListDictionaryInternal}

I would appreciate suggestions that will help resolve this error or suggestions for a different approach to saving and retrieving the entered data.

You are saving the wrong class. Try:

XmlSerializer serializer = new XmlSerializer(typeof(CustomerInfo));

Seems you have missed Serializable attribute. Please check it. Also besides already suggested solutions you can consider using SoapFormatter and BinaryFormatter .

Why are you trying to serialize the CustomerInfoForm ?

Don't you want to serialize CustomerInfo ?

I would use DataContractSerializer instead of XmlSerializer . It is more flexible, and more up to date (at least I think).

var serializer = new DataContractSerializer(typeof(CustomerInfo));

This serializer type is available since .Net 3.0, give it a try, if you are using this version or latter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM