zoukankan      html  css  js  c++  java
  • 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();

            }
        }

  • 相关阅读:
    hdu 5224 Tom and paper 水题
    2015 UESTC 搜索专题N题 韩爷的梦 hash
    2015 UESTC 搜索专题M题 Palindromic String 马拉车算法
    2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
    2015 UESTC 搜索专题J题 全都是秋实大哥 kmp
    2015 UESTC 搜索专题F题 Eight Puzzle 爆搜
    2015 UESTC 搜索专题E题 吴队长征婚 爆搜
    2015 UESTC 搜索专题D题 基爷的中位数 二分
    2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS
    2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索
  • 原文地址:https://www.cnblogs.com/Cindys/p/2728143.html
Copyright © 2011-2022 走看看