将查询字符串解析转换为泛型List的名值集合.

///<summary>
///将查询字符串解析转换为泛型List的名值集合.
///</summary>
///<param name="queryString">查询字符串的值</param>
///<returns>结果</returns>
public static List<NameValueCollection> GetMultipleRecords(string records)
{
List<NameValueCollection> result = new List<NameValueCollection>();
if (string.IsNullOrEmpty(records))
{
return result;
}

string[] items = records.Split('|');
if (items == null || items.Length == 0)
{
return result;
}

foreach (string item in items)
{
NameValueCollection current = GetQueryString(item);
if (current.Count == 0)
{
continue;
}

result.Add(current);
}

return result;
}

///<summary>
///将查询字符串解析转换为名值集合.
///</summary>
///<param name="queryString">查询字符串的值</param>
///<returns>结果</returns>
public static NameValueCollection GetQueryString(string queryString)
{
NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrEmpty(queryString))
{
return result;
}

string key = string.Empty;
string value = string.Empty;
string[] items = queryString.Split('&');
foreach (string item in items)
{
if (!string.IsNullOrEmpty(item))
{
string[] fields = item.Split('=');
if (fields != null && fields.Length == 2)
{
key = fields[0].Trim();
value = fields[1].Trim();

if (string.IsNullOrEmpty(key))
{
continue;
}

result[key] = value;
}
}
}

return result;
}

posted on 2014-01-14 17:20  jameshappy  阅读(2140)  评论(0编辑  收藏  举报