C#序列化与反序列化Xml,利用范型做通用化处理
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
t = (T)xs.Deserialize(fs);
fs.Close();
return t;
}
}
public static void SetToXml<T>(string xmlpath, T t)
{
if (t == null)
return;
using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
xs.Serialize(fs, t);
t = default(T);
fs.Close();
}
}
}
相关文档:
/// <summary>
/// 读取数据集并加入缓存
/// sea 2009-12-11
/// </summary>
&nb ......
C#
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Config/User_yhlx_Jb.xml"));
DataView dv = ds.Tables[0].DefaultView;
//dv.RowFilter = "State=0";
this.DropDownList1.DataSource = dv;
this.DropDownList1.DataTextField = "text";
this ......
protected void Button1_Click(object sender, EventArgs e)
{
//为response(star)节点 和 Cabins(f) cabin节点分别添加个
&nbs ......