C# 如何序列化对象列表 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.IO; 使用System.Linq; 使用系统文本; 使用System.Text.RegularExpressions; 使用System.Windows.Forms; 使用System.Xml.Serialization; 名称空间AppPrueba { 公共部分类Form1:Form { ArrayList listaFilas=新的ArrayList(); 公共表格1() { 初始化组件(); } List emp=新列表(); 列表代理=新列表(); 静态public-void-xml(列表代理) { XmlSerializer serializer=新的XmlSerializer(typeof(List)); TextWriter=newstreamwriter(@“C:\agenda.xml”); serializer.Serialize(textWriter,代理); textWriter.Close(); } 私有无效按钮1\u单击(对象发送者,事件参数e) { openFileDialog1.ShowDialog(这个); 字符串strfilename=openFileDialog1.FileName; Text=strfilename; 如果((strfilename.Trim().Length>0)&&(File.Exists(strfilename))) { 字符串[]readText=File.ReadAllLines(strfilename); if(strfilename.EndsWith(“Agenda.txt”)) { lstLinesBeforeChange.Items.Clear(); foreach(readText中的字符串s) { lstLinesBeforeChange.Items.Add; } } 其他的 { lstLinesBeforeChange.Items.Clear(); foreach(readText中的字符串s) { lstLinesBeforeChange.Items.Add; } } } } 私有void btnModify_单击(对象发送方,事件参数e) { 字符串strfilename=txtPath.Text; 字符串[]readText=File.ReadAllLines(strfilename); if(strfilename.EndsWith(“Agenda.txt”)) { lstLinesAfterChange.Items.Clear(); foreach(readText中的字符串s) { int nroEmp=Convert.ToInt32(s.Substring(0,4)); 字符串nombre=s.Substring(4,15); 字符串telef=s.Substring(19,10); 字符串ciudad=s.Substring(29); 议程unAgenda=新议程(); unAgenda.NroEmp=NroEmp; unAgenda.Nombre=Nombre.TrimStart().TrimEnd(); unAgenda.Telefono=telef; unAgenda.Localidad=ciudad; 代理添加(unAgenda); 代理排序(代表(议程a1、议程a2) { 返回a1.NroEmp.CompareTo(a2.NroEmp); }); } foreach(议程a在agen中) { 字符串agenOrd=a.NroEmp.ToString()+“\t”+a.Nombre+“\t”+a.Telefono+“\t”+a.Localidad; lstLinesAfterChange.Items.Add(agenOrd); } } 其他的 { lstLinesAfterChange.Items.Clear(); foreach(readText中的字符串s) { 字符串[]sSinBarra=s.Split(“|”); int nroEmp=Convert.ToInt32(sSinBarra[0]); 字符串nombre=sSinBarra[1]; 字符串posicion=sSinBarra[2]; int nroOficina=转换为32(sSinBarra[3]); int piso=转换为32(sSinBarra[4]); 字符串fechaIng=sSinBarra[5]; int-dia=转换为32(fechaIng.Substring(0,2)); intmes=Convert.ToInt32(fechaIng.Substring(2,2)); int año=Convert.ToInt32(fechaIng.Substring(4,4)); 字符串fechaIngreso=dia+“/”+mes+“/”+año; fechaIng=fechaIngreso; emplbeados unEmpleado=新的emplbeados(); unEmpleado.NroEmpleado1=nroEmp; unEmpleado.Nombre1=nombre.TrimEnd().TrimStart(); unEmpleado.Posicion=Posicion.TrimEnd().TrimStart(); unEmpleado.NroOficina=NroOficina; unEmpleado.Piso=Piso; unEmpleado.FechaIngreso=FechaIngreso; emp.Add(不复杂); emp.分拣(代表(员工e1、员工e2) { 返回e1.NroEmpleado1.CompareTo(e2.NroEmpleado1); }); } foreach(在emp中雇用em) { 字符串empOrd=em.NroEmpleado1.ToString()+“\t”+em.Nombre1+“\t”+em.Posicion+“\t”+em.NroOficina.ToString() +“\t”+em.Piso.ToString()+“\t”+em.FechaIngreso; lstLinesAfterChange.Items.Add(empOrd); } } } 私有void btnSave\u单击(对象发送方,事件参数e) { //SaveFileDialog saveFileDialog1=新建SaveFileDialog(); //saveFileDialog1.ShowDialog(); //如果(saveFileDialog1.FileName!=“”) //{ //FileStream fs=(FileStream)saveFileDialog1.OpenFile(); //fs.Close(); //} 阿根; } } }
我有这个,我想将这两个列表序列化为xml:C# 如何序列化对象列表 使用系统; 使用系统集合; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.IO; 使用System.Linq; 使用系统文本; 使用System.Text.RegularExpressions; 使用System.Windows.Forms; 使用System.Xml.Serialization; 名称空间AppPrueba { 公共部分类Form1:Form { ArrayList listaFilas=新的ArrayList(); 公共表格1() { 初始化组件(); } List emp=新列表(); 列表代理=新列表(); 静态public-void-xml(列表代理) { XmlSerializer serializer=新的XmlSerializer(typeof(List)); TextWriter=newstreamwriter(@“C:\agenda.xml”); serializer.Serialize(textWriter,代理); textWriter.Close(); } 私有无效按钮1\u单击(对象发送者,事件参数e) { openFileDialog1.ShowDialog(这个); 字符串strfilename=openFileDialog1.FileName; Text=strfilename; 如果((strfilename.Trim().Length>0)&&(File.Exists(strfilename))) { 字符串[]readText=File.ReadAllLines(strfilename); if(strfilename.EndsWith(“Agenda.txt”)) { lstLinesBeforeChange.Items.Clear(); foreach(readText中的字符串s) { lstLinesBeforeChange.Items.Add; } } 其他的 { lstLinesBeforeChange.Items.Clear(); foreach(readText中的字符串s) { lstLinesBeforeChange.Items.Add; } } } } 私有void btnModify_单击(对象发送方,事件参数e) { 字符串strfilename=txtPath.Text; 字符串[]readText=File.ReadAllLines(strfilename); if(strfilename.EndsWith(“Agenda.txt”)) { lstLinesAfterChange.Items.Clear(); foreach(readText中的字符串s) { int nroEmp=Convert.ToInt32(s.Substring(0,4)); 字符串nombre=s.Substring(4,15); 字符串telef=s.Substring(19,10); 字符串ciudad=s.Substring(29); 议程unAgenda=新议程(); unAgenda.NroEmp=NroEmp; unAgenda.Nombre=Nombre.TrimStart().TrimEnd(); unAgenda.Telefono=telef; unAgenda.Localidad=ciudad; 代理添加(unAgenda); 代理排序(代表(议程a1、议程a2) { 返回a1.NroEmp.CompareTo(a2.NroEmp); }); } foreach(议程a在agen中) { 字符串agenOrd=a.NroEmp.ToString()+“\t”+a.Nombre+“\t”+a.Telefono+“\t”+a.Localidad; lstLinesAfterChange.Items.Add(agenOrd); } } 其他的 { lstLinesAfterChange.Items.Clear(); foreach(readText中的字符串s) { 字符串[]sSinBarra=s.Split(“|”); int nroEmp=Convert.ToInt32(sSinBarra[0]); 字符串nombre=sSinBarra[1]; 字符串posicion=sSinBarra[2]; int nroOficina=转换为32(sSinBarra[3]); int piso=转换为32(sSinBarra[4]); 字符串fechaIng=sSinBarra[5]; int-dia=转换为32(fechaIng.Substring(0,2)); intmes=Convert.ToInt32(fechaIng.Substring(2,2)); int año=Convert.ToInt32(fechaIng.Substring(4,4)); 字符串fechaIngreso=dia+“/”+mes+“/”+año; fechaIng=fechaIngreso; emplbeados unEmpleado=新的emplbeados(); unEmpleado.NroEmpleado1=nroEmp; unEmpleado.Nombre1=nombre.TrimEnd().TrimStart(); unEmpleado.Posicion=Posicion.TrimEnd().TrimStart(); unEmpleado.NroOficina=NroOficina; unEmpleado.Piso=Piso; unEmpleado.FechaIngreso=FechaIngreso; emp.Add(不复杂); emp.分拣(代表(员工e1、员工e2) { 返回e1.NroEmpleado1.CompareTo(e2.NroEmpleado1); }); } foreach(在emp中雇用em) { 字符串empOrd=em.NroEmpleado1.ToString()+“\t”+em.Nombre1+“\t”+em.Posicion+“\t”+em.NroOficina.ToString() +“\t”+em.Piso.ToString()+“\t”+em.FechaIngreso; lstLinesAfterChange.Items.Add(empOrd); } } } 私有void btnSave\u单击(对象发送方,事件参数e) { //SaveFileDialog saveFileDialog1=新建SaveFileDialog(); //saveFileDialog1.ShowDialog(); //如果(saveFileDialog1.FileName!=“”) //{ //FileStream fs=(FileStream)saveFileDialog1.OpenFile(); //fs.Close(); //} 阿根; } } },c#,xml,serialization,C#,Xml,Serialization,我有这个,我想将这两个列表序列化为xml: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpr
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace AppPrueba
{
public partial class Form1 : Form
{
ArrayList listaFilas = new ArrayList();
public Form1()
{
InitializeComponent();
}
List<Empleados> emp = new List<Empleados>();
List<Agenda> agen = new List<Agenda>();
static public void SerializeToXML(List<Agenda> agen)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Agenda>));
TextWriter textWriter = new StreamWriter(@"C:\agenda.xml");
serializer.Serialize(textWriter, agen);
textWriter.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(this);
string strfilename = openFileDialog1.FileName;
txtPath.Text = strfilename;
if ((strfilename.Trim().Length > 0) && (File.Exists(strfilename)))
{
string[] readText = File.ReadAllLines(strfilename);
if (strfilename.EndsWith("Agenda.txt"))
{
lstLinesBeforeChange.Items.Clear();
foreach (string s in readText)
{
lstLinesBeforeChange.Items.Add(s);
}
}
else
{
lstLinesBeforeChange.Items.Clear();
foreach (string s in readText)
{
lstLinesBeforeChange.Items.Add(s);
}
}
}
}
private void btnModify_Click(object sender, EventArgs e)
{
string strfilename = txtPath.Text;
string[] readText = File.ReadAllLines(strfilename);
if (strfilename.EndsWith("Agenda.txt"))
{
lstLinesAfterChange.Items.Clear();
foreach (string s in readText)
{
int nroEmp = Convert.ToInt32(s.Substring(0, 4));
string nombre = s.Substring(4, 15);
string telef = s.Substring(19, 10);
string ciudad = s.Substring(29);
Agenda unAgenda = new Agenda();
unAgenda.NroEmp = nroEmp;
unAgenda.Nombre = nombre.TrimStart().TrimEnd();
unAgenda.Telefono = telef;
unAgenda.Localidad = ciudad;
agen.Add(unAgenda);
agen.Sort(delegate(Agenda a1, Agenda a2)
{
return a1.NroEmp.CompareTo(a2.NroEmp);
});
}
foreach (Agenda a in agen)
{
string agenOrd = a.NroEmp.ToString() + "\t" + a.Nombre + "\t" + a.Telefono + "\t" + a.Localidad;
lstLinesAfterChange.Items.Add(agenOrd);
}
}
else
{
lstLinesAfterChange.Items.Clear();
foreach (string s in readText)
{
string[] sSinBarra = s.Split('|');
int nroEmp = Convert.ToInt32(sSinBarra[0]);
string nombre = sSinBarra[1];
string posicion = sSinBarra[2];
int nroOficina = Convert.ToInt32(sSinBarra[3]);
int piso = Convert.ToInt32(sSinBarra[4]);
string fechaIng = sSinBarra[5];
int dia = Convert.ToInt32(fechaIng.Substring(0, 2));
int mes = Convert.ToInt32(fechaIng.Substring(2, 2));
int año = Convert.ToInt32(fechaIng.Substring(4, 4));
string fechaIngreso = dia + "/" + mes + "/" + año;
fechaIng = fechaIngreso;
Empleados unEmpleado = new Empleados();
unEmpleado.NroEmpleado1 = nroEmp;
unEmpleado.Nombre1 = nombre.TrimEnd().TrimStart();
unEmpleado.Posicion = posicion.TrimEnd().TrimStart();
unEmpleado.NroOficina = nroOficina;
unEmpleado.Piso = piso;
unEmpleado.FechaIngreso = fechaIngreso;
emp.Add(unEmpleado);
emp.Sort(delegate(Empleados e1, Empleados e2)
{
return e1.NroEmpleado1.CompareTo(e2.NroEmpleado1);
});
}
foreach (Empleados em in emp)
{
string empOrd = em.NroEmpleado1.ToString() + "\t" + em.Nombre1 + "\t" + em.Posicion + "\t" + em.NroOficina.ToString()
+ "\t" + em.Piso.ToString() + "\t" + em.FechaIngreso;
lstLinesAfterChange.Items.Add(empOrd);
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//saveFileDialog1.ShowDialog();
//if (saveFileDialog1.FileName != "")
//{
// FileStream fs = (FileStream)saveFileDialog1.OpenFile();
// fs.Close();
//}
SerializeToXML(agen);
}
}
}
List emp=new List();
列表代理=新列表();
我使用了在其他教程中找到的SerializeToXML方法,但当我运行它时,会出现一个错误
“错误1可访问性不一致:参数类型”
“System.Collections.Generic.List”不易访问
比方法
'AppPrueba.Form1.SerializeToXML(System.Collec
List<Empleados> emp = new List<Empleados>();
List<Agenda> agen = new List<Agenda>();