XML序列化 - .NET技术 / C#
我不知道有没有办法可以给任意一个类进行序列化和反序列化操作。
C# code:
public static void Serial<T>(T[] items, string path)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
TextWriter writer = new StreamWriter(path);
try
{
xmlSerializer.Serialize(writer, items);
}
finally
{
writer.Close();
}
}
public static T[] Deserial<T>(string path)
{
if (!File.Exists(path)) return new T[0];
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T[]));
FileStream fs = new FileStream(path, FileMode.Open);
T[] items;
try
{
items = (T[])xmlSerializer.Deserialize(fs);
}
finally
{
fs.Close();
}
return items;
}
这个是标准的xml序列化反序列化的方法。也行,就是不能完全的符合我要求。
比如我的类
C# code:
public class A
{
public string P1;
}
我如何做都可以,但如果我序列化生成了"test.xml"文件。
我又扩展了类变为:
C# code:
public class A
{
public string P1;
pulbic string P2;
}
我用反序列化得到的A[]就会都是空的了
相关问答:
<?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical&q ......
我有一个类似的xml的 string,想通过遍历怎么个xml 输出我想要的element的值
xml 为:
<Discover xmlns="urn:schemas-microsoft-com:xml-analysis">
<RequestType>DISCOVER_XML_ME ......
使用ACCESS最大的隐患就是不安全。今天对ACCESS数据库设置了一个密码,必须使用密码才能打开,但是在程序中却无法连接数据库了。大家知道使用用户名和密码,如何连接ACCESS数据库?貌似ACCESS的用户名还不知道?只知 ......
用listview显示出从数据库中查询出的记录,想在最后记录后面添加一条合计行,如何实现!谢谢大家!
绑定数据你应该没问题吧,绑定完以后用一个循环把你要统计数据的列中的数据总和用一个变量接受,然后在循环外面ne ......
A表 有两个字段
id 唯一数字域
InfoTxt text 类型
我现在要把 id 不是14 的所有 InfoTxt字段 文本后面 都加上 'aaa'
按下面执行下来 只有表最后一行加上了 'aaa' ......