xml序列化
来源:互联网 发布:数据挖掘行业应用 编辑:程序博客网 时间:2024/06/05 00:30
//根节点 humanResoure [XmlRoot("humanResource")] public class HumanResource { #region private data private int m_record = 0; //根节点 HumanResource 下面有work节点 将work定义为一个数组类型 private Worker[] m_workers = null; #endregion //xml属性 <humanResour record=''> //AttributeName 属性名称 // [XmlAttribute(AttributeName = "record")] public int Record { get { return m_record; } set { m_record = value; } } //定义一个元素 元素名称为worker workder下面的有number [XmlElement(ElementName = "worker")] public Worker[] workers { get { return m_workers; } set { m_workers = value; } } } //informationItem节点 informationItem下面还有节点 public class Worker { #region private data. private string m_number = null; //定义一个数组对象 Work下面的节点 informationItem private InformationItem[] m_infoItems = null; #endregion //定义一个xml属性 [XmlAttribute("number")] public string Number { get { return m_number; } set { m_number = value; } } //定义一个xml元素 [XmlElement("infoItem")] public InformationItem[] InfoItems { get { return m_infoItems; } set { m_infoItems = value; } } } //work下面的节点 InformationItem public class InformationItem { #region priavte data private string m_name = null; private string m_value = null; #endregion //定义属性名称 在InfomationItem节点下 [XmlAttribute(AttributeName = "name")] public string Name { get { return m_name; } set { m_name = value; } } //定义xml文本节点 [XmlText] public string Value { get { return m_value; } set { m_value = value; } } }
//xml序列化方法 public sealed class ConfigurationManager { //单件模式 定义一个私有的构造函数 private 定义一个私有的类的对象 private static HumanResource m_humanResource = null; private ConfigurationManager() { } public static HumanResource Get(string path) { if (m_humanResource == null) { FileStream fs = null; try { //序列化一个对象 HumanResource XmlSerializer xs = new XmlSerializer(typeof(HumanResource)); fs = new FileStream(path, FileMode.Open, FileAccess.Read); m_humanResource = (HumanResource)xs.Deserialize(fs); fs.Close(); return m_humanResource; } //处理异常 catch { if (fs != null) fs.Close(); throw new Exception("Xml deserialization failed!"); } } else { return m_humanResource; } } //将对象序列化成xml文件 public static void Set(string path,HumanResource humanResource) { FileStream fs = null; if (humanResource == null) { throw new Exception("Parameter humanResource is null!"); } try { //序列化对象 typeof要序列化对象的列名 XmlSerializer xs=new XmlSerializer(typeof(HumanResource)); fs = new FileStream(path,FileMode.Create,FileAccess.Write); xs.Serialize(fs, humanResource); m_humanResource = null; fs.Close(); } catch { if (fs != null) fs.Close(); throw new Exception("Xml serialization failed!"); } } }
序列化后产生xml文件格式如下:
<?xml version="1.0" ?> <humanResource record="2"> <worker number="001"> <infoItem name="name">Michale</infoItem> <infoItem name="sex">male</infoItem> <infoItem name="age">25</infoItem> </worker> <worker number="002"> <infoItem name="name">Surce</infoItem> <infoItem name="sex">male</infoItem> <infoItem name="age">28</infoItem> </worker> </humanResource>
- XML 序列化
- xml序列化集合
- 序列化 XML 数据
- XML序列化
- xml序列化
- XML序列化基础
- XML序列化示例
- xml序列化 ICollection
- xml序列化
- XML 序列化简介
- XML 序列化示例
- XML序列化(一)
- XML序列化(一)
- c#序列化XML
- C# 序列化 xml
- xml序列化
- 自行序列化XML
- 序列化XML
- fedora linux不适用shadow加密的方式
- jQuery的学习
- Form Demo 注解(ST1)
- 你需要知道的 16 个 Linux 服务器监控命令
- 如何输出源文件的标题和目前执行行的行数
- xml序列化
- 源路径选项
- HDU--1004 Let the Balloon Rise
- 从 08年开始写博客,之前一直在 新浪上写,但是好多朋友都在CSDN上,所以今天也搬家了;
- 这种事只有在学校才会发生
- jquery的学习(一)
- MySql使用小结:允许远程访问、更改最大连接数、VARCHAR字段占用的空间、linux下安装workbench
- Volatile
- 内外部类