c#xml序列化问题
c#对类对象是可以序列化的,但是我当我序列化有参数构造函数的类时,就无法序列化该类的对象,报错说该类没有无参的构造函数,无法序列化。例如:publicclassPerso...
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#可以序列化有参数构造函数的类吗?还是不能?如果能那该如何做呢。谁有例子。 展开
例如:
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#可以序列化有参数构造函数的类吗?还是不能?如果能那该如何做呢。谁有例子。 展开
5个回答
展开全部
给你个我写的用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;
}
}
#endregion
#region 判断该路径文件是否存在 如果不存在则创建
/// <summary>
/// 判断该路径文件是否存在 如果不存在则创建
/// </summary>
/// <param name="m_strFullPath">路径</param>
public static void ConfigFile(string m_strFullPath)
{
StreamWriter sr = null;
if (!System.IO.File.Exists(m_strFullPath))
{
try
{
sr = new StreamWriter(m_strFullPath);
sr.WriteLine("<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "utf-8" + "\"" + " ?>");
sr.WriteLine("<configuration>");
sr.WriteLine("<appSettings>");
sr.WriteLine("<add key=" + "\"" + "NameColour" + "\"" + " value=" + "\"" + "Blue" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "InfoColour" + "\"" + " value=" + "\"" + "Black" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsURL" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsEmail" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMoveTime" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMove" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "MoveValue" + "\"" + " value=" + "\"" + "3" + "\"" + "/>");
sr.WriteLine("</appSettings>");
sr.WriteLine("</configuration>");
sr.Close();
}
catch (Exception e)
{
sr.Close();
MessageBox.Show("创建配置文件错误!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
}
#endregion
#region 更新Config文件
/// <summary>
/// 更新Config文件
/// </summary>
/// <param name="p_strKey">键</param>
/// <param name="p_strValue">值</param>
public static void UpdateConfig(string p_strKey, string p_strValue)
{
try
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
ConfigFile(m_strFullPath);
if (File.Exists(m_strFullPath))
{
//处理数据文件为只读的情况
if ((File.GetAttributes(m_strFullPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(m_strFullPath, File.GetAttributes(m_strFullPath) & (~FileAttributes.ReadOnly));
}
}
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("value", p_strValue);
}
}
xmlDoc.Save(m_strFullPath);
//fs.Close();
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
public class ConfigInfo
{
private string strNameColor =ColorTranslator.ToHtml( System.Drawing.Color.Blue);
private string strInfoColor = ColorTranslator.ToHtml(System.Drawing.Color.Black);
private string strIsURL = "1";
private string strIsEmail = "1";
private string strIsMove = "1";
private string strMoveValue = "3";
private string strIsMoveTime = "1";
/// <summary>
/// 是否移除空格
/// </summary>
public bool IsMoveTime
{
get
{
if (strIsMoveTime == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMoveTime = "1";
}
else
{
strIsMoveTime = "0";
}
}
}
/// <summary>
/// 缩进空格
/// </summary>
public decimal MoveValue
{
get
{
return Convert.ToDecimal(strMoveValue);
}
set
{
strMoveValue = value.ToString();
}
}
/// <summary>
/// 是否缩进
/// </summary>
public bool IsMove
{
get
{
if (strIsMove == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMove = "1";
}
else
{
strIsMove = "0";
}
}
}
/// <summary>
/// 邮件是否可以点击
/// </summary>
public bool IsEmail
{
get
{
if (strIsEmail == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsEmail = "1";
}
else
{
strIsEmail = "0";
}
}
}
/// <summary>
/// 网页是否可以点击
/// </summary>
public bool IsURL
{
get
{
if (strIsURL== "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsURL = "1";
}
else
{
strIsURL = "0";
}
}
}
/// <summary>
/// 消息颜色
/// </summary>
public Color InfoColor
{
get
{
return ColorTranslator.FromHtml(strInfoColor);
}
set
{
strInfoColor = ColorTranslator.ToHtml(value);
}
}
/// <summary>
/// 信息颜色
/// </summary>
public Color NameColor
{
get
{
return ColorTranslator.FromHtml(strNameColor); ;
}
set
{
strNameColor = ColorTranslator.ToHtml(value);
}
}
public ConfigInfo()
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlNodeReader objNdRd = new XmlNodeReader(xn);
while (objNdRd.Read())
{
if (objNdRd.GetAttribute("key").IndexOf("NameColour") != -1)
{
this.strNameColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("InfoColour") != -1)
{
this.strInfoColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsURL") != -1)
{
this.strIsURL = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsEmail") != -1)
{
this.strIsEmail = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsMove") != -1)
{
this.strIsMove = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("MoveValue") != -1)
{
this.strMoveValue = objNdRd.GetAttribute("value");
}
}
}
}
public void Update()
{
Config.DelConfig();
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
Config.UpdateConfig("NameColour", strNameColor);
Config.UpdateConfig("InfoColour", strInfoColor);
Config.UpdateConfig("IsURL", strIsURL);
Config.UpdateConfig("IsEmail", strIsEmail);
Config.UpdateConfig("IsMove", strIsMove);
Config.UpdateConfig("MoveValue", strMoveValue);
}
}
}
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;
}
}
#endregion
#region 判断该路径文件是否存在 如果不存在则创建
/// <summary>
/// 判断该路径文件是否存在 如果不存在则创建
/// </summary>
/// <param name="m_strFullPath">路径</param>
public static void ConfigFile(string m_strFullPath)
{
StreamWriter sr = null;
if (!System.IO.File.Exists(m_strFullPath))
{
try
{
sr = new StreamWriter(m_strFullPath);
sr.WriteLine("<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "utf-8" + "\"" + " ?>");
sr.WriteLine("<configuration>");
sr.WriteLine("<appSettings>");
sr.WriteLine("<add key=" + "\"" + "NameColour" + "\"" + " value=" + "\"" + "Blue" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "InfoColour" + "\"" + " value=" + "\"" + "Black" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsURL" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsEmail" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMoveTime" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMove" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "MoveValue" + "\"" + " value=" + "\"" + "3" + "\"" + "/>");
sr.WriteLine("</appSettings>");
sr.WriteLine("</configuration>");
sr.Close();
}
catch (Exception e)
{
sr.Close();
MessageBox.Show("创建配置文件错误!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
}
#endregion
#region 更新Config文件
/// <summary>
/// 更新Config文件
/// </summary>
/// <param name="p_strKey">键</param>
/// <param name="p_strValue">值</param>
public static void UpdateConfig(string p_strKey, string p_strValue)
{
try
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
ConfigFile(m_strFullPath);
if (File.Exists(m_strFullPath))
{
//处理数据文件为只读的情况
if ((File.GetAttributes(m_strFullPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(m_strFullPath, File.GetAttributes(m_strFullPath) & (~FileAttributes.ReadOnly));
}
}
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("value", p_strValue);
}
}
xmlDoc.Save(m_strFullPath);
//fs.Close();
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
public class ConfigInfo
{
private string strNameColor =ColorTranslator.ToHtml( System.Drawing.Color.Blue);
private string strInfoColor = ColorTranslator.ToHtml(System.Drawing.Color.Black);
private string strIsURL = "1";
private string strIsEmail = "1";
private string strIsMove = "1";
private string strMoveValue = "3";
private string strIsMoveTime = "1";
/// <summary>
/// 是否移除空格
/// </summary>
public bool IsMoveTime
{
get
{
if (strIsMoveTime == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMoveTime = "1";
}
else
{
strIsMoveTime = "0";
}
}
}
/// <summary>
/// 缩进空格
/// </summary>
public decimal MoveValue
{
get
{
return Convert.ToDecimal(strMoveValue);
}
set
{
strMoveValue = value.ToString();
}
}
/// <summary>
/// 是否缩进
/// </summary>
public bool IsMove
{
get
{
if (strIsMove == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMove = "1";
}
else
{
strIsMove = "0";
}
}
}
/// <summary>
/// 邮件是否可以点击
/// </summary>
public bool IsEmail
{
get
{
if (strIsEmail == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsEmail = "1";
}
else
{
strIsEmail = "0";
}
}
}
/// <summary>
/// 网页是否可以点击
/// </summary>
public bool IsURL
{
get
{
if (strIsURL== "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsURL = "1";
}
else
{
strIsURL = "0";
}
}
}
/// <summary>
/// 消息颜色
/// </summary>
public Color InfoColor
{
get
{
return ColorTranslator.FromHtml(strInfoColor);
}
set
{
strInfoColor = ColorTranslator.ToHtml(value);
}
}
/// <summary>
/// 信息颜色
/// </summary>
public Color NameColor
{
get
{
return ColorTranslator.FromHtml(strNameColor); ;
}
set
{
strNameColor = ColorTranslator.ToHtml(value);
}
}
public ConfigInfo()
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlNodeReader objNdRd = new XmlNodeReader(xn);
while (objNdRd.Read())
{
if (objNdRd.GetAttribute("key").IndexOf("NameColour") != -1)
{
this.strNameColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("InfoColour") != -1)
{
this.strInfoColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsURL") != -1)
{
this.strIsURL = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsEmail") != -1)
{
this.strIsEmail = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsMove") != -1)
{
this.strIsMove = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("MoveValue") != -1)
{
this.strMoveValue = objNdRd.GetAttribute("value");
}
}
}
}
public void Update()
{
Config.DelConfig();
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
Config.UpdateConfig("NameColour", strNameColor);
Config.UpdateConfig("InfoColour", strInfoColor);
Config.UpdateConfig("IsURL", strIsURL);
Config.UpdateConfig("IsEmail", strIsEmail);
Config.UpdateConfig("IsMove", strIsMove);
Config.UpdateConfig("MoveValue", strMoveValue);
}
}
}
展开全部
要可以序列化的类构造函数不能有参数
你可以改成如下就可以了
public class Person
{
public Person()
{
name = Name;
id = ID;
}
public string name;
public string id;
}
private void button4_Click(object sender, EventArgs e)
{
Person person1 = new Person();
person1.name = "abc";
person1.id = "1234";
XmlSerializer s = new XmlSerializer(typeof(Person));
TextWriter w = new StreamWriter("person1.xml");
s.Serialize(w, person1);
w.Close();
}
你可以改成如下就可以了
public class Person
{
public Person()
{
name = Name;
id = ID;
}
public string name;
public string id;
}
private void button4_Click(object sender, EventArgs e)
{
Person person1 = new Person();
person1.name = "abc";
person1.id = "1234";
XmlSerializer s = new XmlSerializer(typeof(Person));
TextWriter w = new StreamWriter("person1.xml");
s.Serialize(w, person1);
w.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Person
{
public string name;
public string id;
public Person()
{
}
public Person(string Name, string ID)
{
name = Name;
id = ID;
}
}
在这个类里加一个空参数得构造函数,如上所示
{
public string name;
public string id;
public Person()
{
}
public Person(string Name, string ID)
{
name = Name;
id = ID;
}
}
在这个类里加一个空参数得构造函数,如上所示
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
类必须有一个将由 XmlSerializer 序列化的默认构造函数。
参考资料: http://msdn.microsoft.com/zh-cn/library/182eeyhh(VS.80).aspx
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你给类加一个无参数的缺省构造函数就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询