c#xml序列化问题

来源:百度知道 编辑:UC知道 时间:2024/06/25 08:39:14
c#对类对象是可以序列化的,但是我当我序列化有参数构造函数的类时,就无法序列化该类的对象,报错说该类没有无参的构造函数,无法序列化。
例如:
public class Person
{
public Person(string Name, string ID)
{
name = Name;
id = ID;
}
public string name;
public string id;
}
private void button4_Click(object sender, EventArgs e)
{
Person person1=new Person("abc","1234");
XmlSerializer s = new XmlSerializer(typeof(Person));
TextWriter w = new StreamWriter( "person1.xml" );
s.Serialize(w, person1);
w.Close();
}
运行后报错说:
XmlSerialize.Form1.Person 无法序列化,因为它没有无参数的构造函数。
那么c#可以序列化有参数构造函数的类吗?还是不能?如果能那该如何做呢。谁有例子。

给你个我写的用XML存配置的类。你自己看吧。 我看见你的第二题了。仔细看这个。简单的存储都有了

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Win32;
using System.Drawing;
using System.Windows.Forms;

namespace ChartFormat
{
class Config
{
#region 删除配置文件
/// <summary>
/// 删除配置文件
/// </summary>
/// <returns></returns>
public static bool DelConfig()
{
string m_strFullPath = "";
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
try
{
File.Delete(m_strFullPath);
return true;
}
catch
{
return false;
}
}