[XML] Resource帮助类
2014-03-27 17:30 无抿屎的牛牛 阅读(222) 评论(0) 编辑 收藏 举报/// <summary> /// 类说明:Resources /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http://www.sufeinet.com/thread-655-1-1.html[/url] /// </summary> using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; namespace DotNet.Utilities { /// <summary> /// BUResourceManager /// 资源管理器 /// </author> /// </summary> [XmlRoot("resources")] public class Resources { private SortedList<String, String> indexs = new SortedList<String, String>(); [XmlElement("language")] public string language = string.Empty; [XmlElement("displayName")] public string displayName = string.Empty; [XmlElement("version")] public string version = string.Empty; [XmlElement("author")] public string author = string.Empty; [XmlElement("description")] public string description = string.Empty; [XmlElement("items", typeof(Items))] public Items items; public void createIndex() { indexs.Clear(); if (items == null) { return; } indexs = new SortedList<String, String>(items.items.Length); for (int i = 0; i < items.items.Length; i++) { #if DEBUG try { [i] indexs.Add(items.items.key, items.items.value); } catch { throw (new Exception(items.items.key + items.items.value)); } #else indexs.Add(items.items.key, items.items.value); #endif } } public string Get(string key) { if (!indexs.ContainsKey(key)) { return string.Empty; } return indexs[key]; } /// <summary> /// JiRiGaLa 2007.05.02 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool Set(string key, string value) { if (!indexs.ContainsKey(key)) { return false; } indexs[key] = value; for (int i = 0; i < items.items.Length; i++) { if (items.items.key == key) { items.items.value = value; break; } } return true; } } public class Items { [XmlElement("item", typeof(Item))] public Item[] items; } public class Item { [XmlAttribute("key")] public string key = string.Empty; [XmlText] public string value = string.Empty; } internal class ResourcesSerializer { public static Resources DeSerialize(string filePath) { System.Xml.Serialization.XmlSerializer XmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Resources)); System.IO.FileStream FileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open); Resources Resources = XmlSerializer.Deserialize(FileStream) as Resources; FileStream.Close(); return Resources; } public static void Serialize(string filePath, Resources Resources) { System.Xml.Serialization.XmlSerializer XmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Resources)); System.IO.FileStream FileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create); XmlSerializer.Serialize(FileStream, Resources); FileStream.Close(); } } }