新聞中心
.NET對象的XML序列化和反序列化是如何實現(xiàn)的呢?通過下面實例中的xml schema 描述了一個簡單的人力資源信息,詳細(xì)向你介紹.NET對象的XML序列化和反序列化的實現(xiàn)過程其中包含了XML的大部分格式,如XML元素相互嵌套, XML元素既有元素值,又有屬性值。

沙坪壩ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
XML序列化和反序列化實現(xiàn)1. 待序列化的類層次結(jié)構(gòu)
- [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序列化和反序列化實現(xiàn)2. 序列化生成的xml結(jié)構(gòu)
- ﹤?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序列化和反序列化實現(xiàn)3. 利用XmlSerializer類進(jìn)行序列化和反序列化的實現(xiàn)
一般利用緩存機制實現(xiàn)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序列化和反序列化實現(xiàn)的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)XML序列化和反序列化的實現(xiàn)有所幫助。
【編輯推薦】
- 淺析C# XML解析實例
- C# XML解析方法實戰(zhàn)剖析
- C# XML解析方式實例解析
- 簡述C# XML解析方法的特點及應(yīng)用
- .NET對象的XML序列化和反序列化概念淺析
網(wǎng)站標(biāo)題:.NET對象的XML序列化和反序列化實例詳解
URL鏈接:http://www.5511xx.com/article/dhooddp.html


咨詢
建站咨詢
