C# 在c中使用serialize加载和保存文件#
我创建了一个代码,其中属性被编写为:C# 在c中使用serialize加载和保存文件#,c#,serialization,deserialization,loading,file-handling,C#,Serialization,Deserialization,Loading,File Handling,我创建了一个代码,其中属性被编写为: namespace ContactManagement { public class Contact { public int Id{get; set;} public string Name { get; set; } public byte Age { get; set; } public DateTime Date { get; set; } public
namespace ContactManagement
{
public class Contact
{
public int Id{get; set;}
public string Name { get; set; }
public byte Age { get; set; }
public DateTime Date { get; set; }
public string City { get; set; }
public Contact()
{
}
public Contact(int id, string name, byte age, DateTime date, string city)
{
this.Id = id;
this.Name = name;
this.Age = age;
this.Date = date;
this.City = city;
}
}
}
在写了这篇文章之后,我编写了一段代码,其中写入了文件处理:
namespace ContactManagement
{
public class ContactDB
{
string fileAddress = @"e://ContactDB.xml";
static void Main(string[] args)
{
ContactDB dbObj = new ContactDB();
List<Contact> lstContact = new List<Contact>(){
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
};
dbObj.Save(lstContact);
dbObj.Load();
Console.ReadKey();
}
public void Save(List<Contact> lstContact)
{
FileStream wfs = new FileStream(fileAddress, FileMode.Create, FileAccess.Write);
XmlSerializer serialobj = new XmlSerializer(typeof(ContactDB));
serialobj.Serialize(wfs, lstContact);
wfs.Close();
}
public List<Contact> Load()
{
List<Contact> listofContact = new List<Contact>();
FileStream rfs = new FileStream(fileAddress, FileMode.Open, FileAccess.Read);
XmlSerializer newserial = new XmlSerializer(typeof(ContactDB));
string line;
using (StreamReader sr = new StreamReader(fileAddress))
{
while ((line = sr.ReadLine())!= null)
{
Console.WriteLine(line);
}
}
return listofContact;
}
}
}
名称空间联系人管理
{
公共类联系人数据库
{
字符串fileAddress=@“e://ContactDB.xml”;
静态void Main(字符串[]参数)
{
ContactDB dbObj=新ContactDB();
List lstContact=新列表(){
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
};
dbObj.Save(lstContact);
dbObj.Load();
Console.ReadKey();
}
公共作废保存(联系人列表)
{
FileStream wfs=newfilestream(fileAddress,FileMode.Create,FileAccess.Write);
XmlSerializer serialobj=新的XmlSerializer(typeof(ContactDB));
serialobj.Serialize(wfs,LST联系人);
wfs.Close();
}
公共列表加载()
{
List-listofContact=新列表();
FileStream rfs=新的FileStream(fileAddress,FileMode.Open,FileAccess.Read);
XmlSerializer newserial=新的XmlSerializer(typeof(ContactDB));
弦线;
使用(StreamReader sr=新的StreamReader(文件地址))
{
而((line=sr.ReadLine())!=null)
{
控制台写入线(行);
}
}
返回联系人列表;
}
}
}
问题是编译后,我收到一个错误:生成XML文档时出错。您正在为
ContactDB
创建序列化程序,但正在传递列表的实例。除非前者扩展后者,否则这是行不通的
您需要为List
创建序列化程序,并继续将列表传递给序列化程序:
public void Save(List<Contact> lstContact)
{
FileStream wfs = new FileStream(fileAddress, FileMode.Create, FileAccess.Write);
XmlSerializer serialobj = new XmlSerializer(typeof(List<Contact>));
serialobj.Serialize(wfs, lstContact);
wfs.Close();
}
以下是一个适合我的解决方案:
List<Contact> lstContact = new List<Contact>(){
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
};
String xml;
using (MemoryStream str = new MemoryStream())
{
XmlSerializer ser = new XmlSerializer(typeof(List<Contact>));
ser.Serialize(str, lstContact);
xml = Encoding.Default.GetString(str.ToArray());
}
List<Contact> lstContact2;
using (MemoryStream str = new MemoryStream(Encoding.Default.GetBytes(xml)))
{
XmlSerializer ser = new XmlSerializer(typeof(List<Contact>));
lstContact2 = (List<Contact>)ser.Deserialize(str);
}
List lstContact=新列表(){
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
新联系人(){Name=“hello”,年龄=10,日期=new DateTime(2017,03,12),City=“test”},
};
字符串xml;
使用(MemoryStream str=new MemoryStream())
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
序列序列化(str,lstContact);
xml=Encoding.Default.GetString(str.ToArray());
}
清单2;
使用(MemoryStream str=newmemoryStream(Encoding.Default.GetBytes(xml)))
{
XmlSerializer ser=新的XmlSerializer(typeof(List));
lstContact2=(列表)序列反序列化(str);
}
retVal=(列表)xml.Deserialize(ReadFs) 它是否说明了错误是什么?这是错误:System.Xml.dll中发生了类型为“System.InvalidOperationException”的未处理异常其他信息:生成Xml文档时出错。是否存在InnerException?它应该给你更多的信息。当它说“有一个错误”时,应该有关于错误是什么以及发生在哪里的信息。您是否尝试在中设置断点?或者将保存代码包装在try/catch中,throw-ex代码>并在上面设置一个断点。我做到了。我试着抓住,最后也抓住了。它产生的错误又是一样的。它正在捕获列表的计数,即8。但它没有显示列表,显示的错误是:生成XML文档时出错。即使在我为联系人创建序列化程序并在其中创建属性时,我遇到了相同的错误:System.Xml.dll中发生了类型为“System.InvalidOperationException”的未处理异常。其他信息:生成Xml文档时出错。@ApoorvaSharma我已扩展了答案,以提供您可以执行的操作的示例。public void Save(列出联系人){FileStream wfs=newfilestream(fileAddress,FileMode.Create,FileAccess.Write);XmlSerializer serialobj=newxmlserializer(typeof(List));尝试{serialobj.Serialize(wfs,lstContact);}catch{throw;}最后{wfs.Close();}}这是我写的。但是错误现在出现在XML文档中(2,2)@ApoorvaSharma本应有效。我已添加了完整的解决方案。请与之比较。@ApoorvaSharma我已从您的评论中复制了该方法,它对我有效。您到底从哪里得到了新错误?我尝试了您的上述代码。并在separ中进行反序列化时向我显示了错误
List<Contact> lstContact = new List<Contact>(){
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
new Contact(){ Name="hello", Age=10, Date=new DateTime(2017,03,12), City="test"},
};
String xml;
using (MemoryStream str = new MemoryStream())
{
XmlSerializer ser = new XmlSerializer(typeof(List<Contact>));
ser.Serialize(str, lstContact);
xml = Encoding.Default.GetString(str.ToArray());
}
List<Contact> lstContact2;
using (MemoryStream str = new MemoryStream(Encoding.Default.GetBytes(xml)))
{
XmlSerializer ser = new XmlSerializer(typeof(List<Contact>));
lstContact2 = (List<Contact>)ser.Deserialize(str);
}