请教C#下如何序列化自定义结构或类
答案:2 悬赏:10 手机版
解决时间 2021-01-27 07:31
- 提问者网友:爱了却不能说
- 2021-01-27 04:01
请教C#下如何序列化自定义结构或类
最佳答案
- 五星知识达人网友:毛毛
- 2021-01-27 05:26
C#可以序列化,一般可以转成[二进制序列化][SOAP序列化][XML 序列化],就举个XML的例子吧。写了一个类并序列化和反序。
1、我写的是控制台程序
2、要引用using System.Xml.Serialization;using System.IO;
3、Person类中,Address属性我定义了忽略,他不会被序列化,所以你打开XML是看不到这个字段的。
4、文件我写死了,是D盘的1234.xml,你可以改。
=====代码====
[Serializable]
public class Person
{
///
/// 姓名
///
public string Name { get; set; }
///
/// 年龄
///
public int Age { get; set; }
///
/// 地址,属性指这个字段不序列化
///
[XmlIgnore]
public string Address { get; set; }
}上面是个类,要序列化,就要写特性[Serializable],下面是我写的两个方法
///
/// 序列化
///
///
///
static void CreateSerialize(Type type, object obj)
{
XmlSerializer xs = new XmlSerializer(type);
Stream stream = new FileStream(@"d:\1234.XML", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
xs.Serialize(stream, obj);
stream.Close();
}
///
/// 反序
///
///
///
static object XMLDeserialize(Type type)
{
XmlSerializer xs = new XmlSerializer(type);
Stream stream = new FileStream(@"d:\\1234.XML", FileMode.Open, FileAccess.Read, FileShare.Read);
var str = xs.Deserialize(stream);
stream.Close();
return str;
}你可以在主程序中Main()中调用上面的方法,我给了例子。
//建立几个对象
List list = new List()
{
new Person(){ Name = "张三", Age = 18, Address = "A小区" },
new Person(){ Name = "李四", Age = 21, Address = "B小区" },
new Person(){ Name = "王五", Age = 23, Address = "C小区" },
new Person(){ Name = "赵六", Age = 17, Address = "D小区" },
new Person(){ Name = "钱七", Age = 22, Address = "E小区" },
};
foreach (var obj in list)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address));
}
Console.WriteLine("序列化开始....");
CreateSerialize(typeof(List), list);
Console.WriteLine("序列化结束....");
Console.WriteLine("反序列化开始....");
var dList = XMLDeserialize(typeof(List)) as List;
foreach (var obj in dList)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address));
}
Console.WriteLine("反序列化结束....");
Console.ReadKey();这只是个简单的例子。
1、我写的是控制台程序
2、要引用using System.Xml.Serialization;using System.IO;
3、Person类中,Address属性我定义了忽略,他不会被序列化,所以你打开XML是看不到这个字段的。
4、文件我写死了,是D盘的1234.xml,你可以改。
=====代码====
[Serializable]
public class Person
{
///
/// 姓名
///
public string Name { get; set; }
///
/// 年龄
///
public int Age { get; set; }
///
/// 地址,属性指这个字段不序列化
///
[XmlIgnore]
public string Address { get; set; }
}上面是个类,要序列化,就要写特性[Serializable],下面是我写的两个方法
///
/// 序列化
///
///
///
static void CreateSerialize(Type type, object obj)
{
XmlSerializer xs = new XmlSerializer(type);
Stream stream = new FileStream(@"d:\1234.XML", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
xs.Serialize(stream, obj);
stream.Close();
}
///
/// 反序
///
///
///
static object XMLDeserialize(Type type)
{
XmlSerializer xs = new XmlSerializer(type);
Stream stream = new FileStream(@"d:\\1234.XML", FileMode.Open, FileAccess.Read, FileShare.Read);
var str = xs.Deserialize(stream);
stream.Close();
return str;
}你可以在主程序中Main()中调用上面的方法,我给了例子。
//建立几个对象
List
{
new Person(){ Name = "张三", Age = 18, Address = "A小区" },
new Person(){ Name = "李四", Age = 21, Address = "B小区" },
new Person(){ Name = "王五", Age = 23, Address = "C小区" },
new Person(){ Name = "赵六", Age = 17, Address = "D小区" },
new Person(){ Name = "钱七", Age = 22, Address = "E小区" },
};
foreach (var obj in list)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address));
}
Console.WriteLine("序列化开始....");
CreateSerialize(typeof(List
Console.WriteLine("序列化结束....");
Console.WriteLine("反序列化开始....");
var dList = XMLDeserialize(typeof(List
foreach (var obj in dList)
{
Console.WriteLine("Name = " + obj.Name + "\tAge = " + obj.Age + "\t地址 = " + (String.IsNullOrEmpty(obj.Address) ? "空" : obj.Address));
}
Console.WriteLine("反序列化结束....");
Console.ReadKey();这只是个简单的例子。
全部回答
- 1楼网友:十鸦
- 2021-01-27 05:59
不可以
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |