通用类 NVCSpecialized 序列和反序列化

/// <summary>
    /// 序列和反序列化
    /// </summary>
    public class NVCSpecialized
    {

        /// <summary>
        /// 将字符串序列化<see cref="System.Collections.Specialized.NameValueCollection"/>类型,第一个为键,第二位与其相对应的值。
        /// </summary>
        /// <param name="keys">NameValueCollection中的keys键。</param>
        /// <param name="values">NameValueCollection中的keys键对应的value。</param>
        /// <returns>基于key-value格式的NameValeCollection集合对象。</returns>
        /// <example>
        /// string keys = "key1:S:0:3:key2:S:3:2:";
        /// string values = "12345";
        /// 则这个所得的NameValueCollection结果中包含两个键(key1和key2),他们所对应的值分别为(123和45)。
        /// 其中:"key1:S:0:3"的key1表示键,S表示字符类型,0表示values起始的位置,3表示values结束的位置。
        /// </example>
        public static NameValueCollection ConvertToNVC(string keys, string values)
        {
            NameValueCollection nvc = new NameValueCollection();

            if (keys != null && values != null && keys.Length > 0 && values.Length > 0)
            {
                char[] splitter = new char[1] { ':' };
                string[] keyNames = keys.Split(splitter);

                for (int i = 0; i < (keyNames.Length / 4); i++)
                {
                    int start = int.Parse(keyNames[(i * 4) + 2], CultureInfo.InvariantCulture);
                    int len = int.Parse(keyNames[(i * 4) + 3], CultureInfo.InvariantCulture);
                    string key = keyNames[i * 4];

                    if (((keyNames[(i * 4) + 1] == "S") && (start >= 0)) && (len > 0) && (values.Length >= (start + len)))
                    {
                        nvc[key] = values.Substring(start, len);
                    }
                }
            }

            return nvc;
        }

        /// <summary>
        ///  基于NameValueCollection对象序列化成keys和values的字符串。
        /// </summary>
        /// <param name="nvc">用于序列化的NameValueCollection对象。</param>
        /// <param name="keys">ref格式的参数keys将转化为NameValueCollection中的key。</param>
        /// <param name="values">ref格式的参数values将转化为NameValueCollection中的value。</param>
        public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
        {
            if (nvc == null || nvc.Count == 0)
                return;

            StringBuilder sbKey = new StringBuilder();
            StringBuilder sbValue = new StringBuilder();

            int index = 0;
            foreach (string key in nvc.AllKeys)
            {
                if (key.IndexOf(':') != -1)
                    throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");

                string v = nvc[key];
                if (!IsNullorEmpty(v))
                {
                    sbKey.AppendFormat("{0}:S:{1}:{2}:", key, index, v.Length);
                    sbValue.Append(v);
                    index += v.Length;
                }
            }
            keys = sbKey.ToString();
            values = sbValue.ToString();
        }

        public static bool IsNullorEmpty(string text)
        {
            return text == null || text.Trim() == string.Empty;
        }
    }

 

posted @ 2012-08-29 16:01  暗尘掩月  阅读(162)  评论(0编辑  收藏  举报