xml 序列化操作

简介:

第一,首先有一个要被序列化成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;

相关文章
|
XML JSON 数据格式
.NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
.NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
240 0
|
XML JavaScript 数据格式
【Groovy】xml 序列化 ( 使用 StreamingMarkupBuilder 生成 xml 数据 | mkp.xmlDeclaration() 生成 xml 版本数据 )
【Groovy】xml 序列化 ( 使用 StreamingMarkupBuilder 生成 xml 数据 | mkp.xmlDeclaration() 生成 xml 版本数据 )
338 0
【Groovy】xml 序列化 ( 使用 StreamingMarkupBuilder 生成 xml 数据 | mkp.xmlDeclaration() 生成 xml 版本数据 )
|
XML C# 数据格式