第一,首先有一个要被序列化成xml的类
public class XmlObject
{
private string _UserName="hello";
public string UserName
{
get
{
return _UserName;
}
set
{
_UserName = value;
}
}
//序列化时.方法是没被序列化的.这里算白写了
protected string GetSomeThing(string WhosName)
{
return WhosName;
}
}
第二:对象---->xml
//执行后:会生成一个pp.xml,当然这个文件名是自己取的
XmlObjectxo = new XmlObject();
StreamWriter sw = new StreamWriter(Server.MapPath("pp.xml"));
XmlSerializer xs = new XmlSerializer(typeof(XmlObject));
xs.Serialize(sw,xo);
sw.Close();
第三: xml---->对象
从第二部的pp.xml转成对象xo
StreamReader reader = File.OpenText(Server.MapPath("pp.xml"));
XmlSerializer serializer = new XmlSerializer(typeof(XmlObject));
XmlObjectxo= (XmlObject)serializer.Deserialize(reader);
reader.Close();
return xo;
原文链接:https://www.f2er.com/xml/294632.html