简体   繁体   English

将C#表单序列化为XML

[英]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 此简单表单在表单上具有10个文本框,这些文本框绑定到表单中的属性

  • Customer name 顾客姓名
  • PhonePrimary.phoneNumber PhonePrimary.phoneNumber
  • PhonePrimary.phoneType PhonePrimary.phoneType
  • PhonePrimary.TextMessageOK PhonePrimary.TextMessageOK
  • PhoneDaytime.phoneNumber PhoneDaytime.phoneNumber
  • PhoneDaytime.phoneType PhoneDaytime.phoneType
  • PhoneDaytime.TextMessageOK PhoneDaytime.TextMessageOK
  • PhoneEvening.phoneNumber PhoneEvening.phoneNumber
  • PhoneEvening.phoneType PhoneEvening.phoneType
  • PhoneEvening.TextMessageOK 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. 填写完这些值后,我想创建一个XML文件来保存这些值,以便以后可以检索它们。

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} 最内部的异常错误是:{“无法序列化System.ComponentModel.ISite类型的成员System.ComponentModel.Component.Site,因为它是一个接口。”}数据:{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. 似乎您错过了Serializable属性。 Please check it. 请检查一下。 Also besides already suggested solutions you can consider using SoapFormatter and BinaryFormatter . 此外,除了已经建议的解决方案之外,您还可以考虑使用SoapFormatterBinaryFormatter

Why are you trying to serialize the CustomerInfoForm ? 您为什么要序列化CustomerInfoForm

Don't you want to serialize CustomerInfo ? 您不想序列化CustomerInfo吗?

I would use DataContractSerializer instead of XmlSerializer . 我将使用DataContractSerializer而不是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. 从.Net 3.0开始,此序列化器类型可用。如果使用此版本或更高版本,请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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