Mono for Android 实用ISharedPreferences 保存配置

 public class Option
    {
        public string OptionName { get; set; }
        public string URL { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

    }

    public class CCTVConfig
    {
        static ISharedPreferences preference = null;

        public static void Save(Activity a, List<Option> options)
        {
            preference = a.GetSharedPreferences("CCTVConfig", FileCreationMode.Private);
            Android.Content.ISharedPreferencesEditor editor = preference.Edit();
            editor.PutString("CCTVConfigOptions", GetXmlStr(options));
            editor.Commit();
        }

        public static List<Option> Load(Activity a)
        {
            preference = a.GetSharedPreferences("CCTVConfig", FileCreationMode.Private);
            if (preference == null)
                return null;
            List<Option> options = new List<Option>();
            string xmlStr = preference.GetString("CCTVConfigOptions", "");
            if (!string.IsNullOrEmpty(xmlStr))
            {
                XDocument xDoc = XDocument.Parse(xmlStr);
                foreach (XElement element in xDoc.Element("CCTVSettings").Descendants("CCTVSetting"))
                {
                    Option en = new Option();
                    en.URL = element.Element("URL").Value;
                    en.Username = element.Element("Username").Value;
                    en.Password = element.Element("Password").Value;
                    en.OptionName = element.Element("OptionName").Value;
                    options.Add(en);
                }
            }
            return options;
        }

        static string GetXmlStr(List<Option> options)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><CCTVSettings>");

            if (options != null)
            {
                string resultXml = "<CCTVSetting><OptionName>{0}</OptionName><URL>{1}</URL><Username>{2}</Username><Password>{3}</Password></CCTVSetting>";

                foreach (var o in options)
                {
                    string temp = string.Format(resultXml, FilterXml(o.OptionName), FilterXml(o.URL), FilterXml(o.Username), FilterXml(o.Password));
                    sb.Append(temp);
                }
                sb.Append("</CCTVSettings>");
                return sb.ToString();
            }
            return "";
        }

        /// <summary>
        /// 过滤XML字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        static string FilterXml(string source)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(source);
            sb.Replace("&lt;", "<");
            sb.Replace("&gt;", ">");
            sb.Replace("&apos;", "'");
            sb.Replace("&quot;", "\"");
            sb.Replace("&amp;", "&");

            sb.Replace("&", "&amp;");
            sb.Replace("<", "&lt;");
            sb.Replace(">", "&gt;");
            sb.Replace("'", "&apos;");
            sb.Replace("\"", "&quot;");

            return sb.ToString();

        }
    }

posted @ 2012-10-17 17:42  静艳  阅读(332)  评论(0编辑  收藏  举报