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();

            }
        }

  • 相关阅读:
    缓存雪崩与缓存穿透
    读取表中最大值
    使用vscode在谷歌上运行代码
    elment 中tree组件展开所有和收缩所有节点
    深度系统商店提示无法安装软件依赖错误
    诗词,理解,品论
    《45个十分钟读懂资本论》原文、适合朗读版和个人见解
    《论持久战》全文
    OSError: [WinError 126] 找不到指定的模块。
    C++ 获取序列最大(或最小)的 N 个元素
  • 原文地址:https://www.cnblogs.com/Cindys/p/2728143.html
Copyright © 2011-2022 走看看