c#xml序列化时,怎么设置xml的编码,我要设置
1个回答
展开全部
Serializable]
public class Employee {
public String Name { get; set; }
public String DepartmentName { get; set; }
public String Post { get; set; }
[XmlIgnore]
public int Age { get; set; }
}
class Program{
static void Main(string[] args){
Employee firstEmp = new Employee() { Name = "光头强", DepartmentName = "大数据技术中心", Post = "人工智能专家" };
Employee secondEmp = null;
XmlSerializer xs = new XmlSerializer(typeof(Employee));
// 序列化为xml
using (TextWriter tw=new StreamWriter("employee.xml")){
xs.Serialize(tw, firstEmp);
tw.Flush();
tw.Close();
}
// 把xml反列化对象
using (TextReader tr = new StreamReader("employee.xml")){
secondEmp= xs.Deserialize(tr) as Employee;
tr.Close();
}
if (secondEmp != null){
Console.WriteLine("Name="+secondEmp.Name+" Department Name="+secondEmp.DepartmentName+" Post="+secondEmp.Name);
}
}
}
public class Employee {
public String Name { get; set; }
public String DepartmentName { get; set; }
public String Post { get; set; }
[XmlIgnore]
public int Age { get; set; }
}
class Program{
static void Main(string[] args){
Employee firstEmp = new Employee() { Name = "光头强", DepartmentName = "大数据技术中心", Post = "人工智能专家" };
Employee secondEmp = null;
XmlSerializer xs = new XmlSerializer(typeof(Employee));
// 序列化为xml
using (TextWriter tw=new StreamWriter("employee.xml")){
xs.Serialize(tw, firstEmp);
tw.Flush();
tw.Close();
}
// 把xml反列化对象
using (TextReader tr = new StreamReader("employee.xml")){
secondEmp= xs.Deserialize(tr) as Employee;
tr.Close();
}
if (secondEmp != null){
Console.WriteLine("Name="+secondEmp.Name+" Department Name="+secondEmp.DepartmentName+" Post="+secondEmp.Name);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询