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

            }
        }

  • 相关阅读:
    动态代理:JDK动态代理和CGLIB代理的区别
    spring启动component-scan类扫描加载,以及@Resource,postConstruct等等注解的解析生效源码
    spring启动component-scan类扫描加载过程---源码分析
    spring源码分析之spring-core asm概述
    Spring组件扫描 <context:component-scan/>
    【OSGI】1.初识OSGI-到底什么是OSGI
    superrvisor application config ini
    doris 0.9.0版本docker镜像制作与使用
    Docker系列09:搭建Harbor本地镜像仓库
    Docker系列08:容器监控
  • 原文地址:https://www.cnblogs.com/Cindys/p/2728143.html
Copyright © 2011-2022 走看看