.NET对象的XML序列化和反序列化是如何实现的呢?通过下面实例中的xml schema 描述了一个简单的人力资源信息,详细向你介绍.NET对象的XML序列化和反序列化的实现过程其中包含了XML的大部分格式,如XML元素相互嵌套, XML元素既有元素值,又有属性值。
沙坪坝ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
XML序列化和反序列化实现1. 待序列化的类层次结构
- [XmlRoot("humanResource")]
- public class HumanResource
- {
- #region private data.
- private int m_record = 0;
- private Worker[] m_workers = null;
- #endregion
- [XmlAttribute(AttributeName="record")]
- public int Record
- {
- get { return m_record; }
- set { m_record = value; }
- }
- [XmlElement(ElementName="worker")]
- public Worker[] Workers
- {
- get { return m_workers; }
- set { m_workers = value; }
- }
- }
- public class Worker
- {
- #region private data.
- private string m_number = null;
- private InformationItem[] m_infoItems = null;
- #endregion
- [XmlAttribute("number")]
- public string Number
- {
- get { return m_number; }
- set { m_number = value; }
- }
- [XmlElement("infoItem")]
- public InformationItem[] InfoItems
- {
- get { return m_infoItems; }
- set { m_infoItems = value; }
- }
- }
- public class InformationItem
- {
- #region private data.
- private string m_name = null;
- private string m_value = null;
- #endregion
- [XmlAttribute(AttributeName = "name")]
- public string Name
- {
- get { return m_name; }
- set { m_name = value; }
- }
- [XmlText]
- public string Value
- {
- get { return m_value; }
- set { m_value = value; }
- }
- }
XML序列化和反序列化实现2. 序列化生成的xml结构
- ﹤?xml version="1.0" ?﹥
- ﹤humanResource
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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序列化和反序列化实现3. 利用XmlSerializer类进行序列化和反序列化的实现
一般利用缓存机制实现xml文件只解析一次。
- public sealed class ConfigurationManager
- {
- private static HumanResource m_humanResource = null;
- private ConfigurationManager(){}
- public static HumanResource Get(string path)
- {
- if (m_humanResource == null)
- {
- FileStream fs = null;
- try
- {
- 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;
- }
- }
- public static void Set(
- string path, HumanResource humanResource)
- {
- if (humanResource == null)
- throw new Exception("Parameter humanResource is null!");
- FileStream fs = null;
- try
- {
- 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序列化和反序列化的实现有所帮助。
【编辑推荐】
网站标题:.NET对象的XML序列化和反序列化实例详解
分享路径:http://www.shufengxianlan.com/qtweb/news5/205.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联