您好,登錄后才能下訂單哦!
.如何理解.NET對象的XML序列化和反序列化,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
.NET對象的XML序列化和反序列化是如何實現的呢?通過下面實例中的xml schema 描述了一個簡單的人力資源信息,詳細向你介紹.NET對象的XML序列化和反序列化的實現過程其中包含了XML的大部分格式,如XML元素相互嵌套, XML元素既有元素值,又有屬性值。
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!"); } } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。