WebService特性和数组类型解析
系列文章导航:
(二)数据类型
Web服务可以使用基本的数据类型,但除了这些还可以使用数据和基本类型的ArrayLists,由于Web服务是通过XML与客户端之间传递的,那么无论使用参数或者返回值。
注:使用数组的主要限制是,必须在设计的时候,你就要知道数组中元素的个数,如果元素的数量是动态的,那么就应该使用ArrayList。
ArrayList包含在System.Collection命名空间中,所以要使用它,必须引用。
我做了个例子来试验这种说法,还是用我在上一篇文章中的二维数组。
//构造并填充info数组
string[,] strStuInfo ={
{"200511020120","贰零零五届","人文法律学院","社会工作","(1)班","贺无悔","1"},
{"200511020121","贰零零五届","人文法律学院","社会工作","(1)班","贺梁需","2"},
{"200511010122","贰零零五届","人文法律学院","法律专业","(1)班","贺陆磊","3"},
{"200511010220","贰零零五届","人文法律学院","法律专业","(2)班","贺白灵","4"},
{"200511010221","贰零零五届","人文法律学院","法律专业","(2)班","李剑付","5"},
{"200511020222","贰零零五届","人文法律学院","社会工作","(2)班","李敬意","6"},
{"200511100120","贰零零五届","化工生物学院","生物制药","(1)班","李黄兴","7"},
{"200511100221","贰零零五届","化工生物学院","生物制药","(2)班","李蕾蕾","8"},
{"200511100322","贰零零五届","化工生物学院","生物制药","(3)班","杜白冰","9"},
{"200611120120","贰零零六届","化工生物学院","发酵工程","(1)班","杜书并","10"},
{"200611120121","贰零零六届","化工生物学院","发酵工程","(1)班","杜小鱼","11"},
{"200611120421","贰零零六届","化工生物学院","发酵工程","(4)班","杜声声","12"},
};
string[,] strStuInfo ={
{"200511020120","贰零零五届","人文法律学院","社会工作","(1)班","贺无悔","1"},
{"200511020121","贰零零五届","人文法律学院","社会工作","(1)班","贺梁需","2"},
{"200511010122","贰零零五届","人文法律学院","法律专业","(1)班","贺陆磊","3"},
{"200511010220","贰零零五届","人文法律学院","法律专业","(2)班","贺白灵","4"},
{"200511010221","贰零零五届","人文法律学院","法律专业","(2)班","李剑付","5"},
{"200511020222","贰零零五届","人文法律学院","社会工作","(2)班","李敬意","6"},
{"200511100120","贰零零五届","化工生物学院","生物制药","(1)班","李黄兴","7"},
{"200511100221","贰零零五届","化工生物学院","生物制药","(2)班","李蕾蕾","8"},
{"200511100322","贰零零五届","化工生物学院","生物制药","(3)班","杜白冰","9"},
{"200611120120","贰零零六届","化工生物学院","发酵工程","(1)班","杜书并","10"},
{"200611120121","贰零零六届","化工生物学院","发酵工程","(1)班","杜小鱼","11"},
{"200611120421","贰零零六届","化工生物学院","发酵工程","(4)班","杜声声","12"},
};
我想做的就是创建一个名叫GetAllStuInfo的Web方法,它的参数是字符串(学生的名字),该字符串跟二维数组中存储的学生名字进行比较(是姓还是姓名中包含的字符串,这个不考虑),服务会返回所有包含匹配字符串的所有信息。
代码如下:
[WebMethod(Description = "返回查询学生名字中包含参数字符串的学生信息")]
public ArrayList GetAllStuInfo(string strStuName)
{
ArrayList arrAllStuInfo = new ArrayList();
//遍历数组,寻找匹配的学生姓名
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (strStuInfo[i, 5].IndexOf(strStuName.Trim()) >= 0)
{
arrAllStuInfo.Add(strStuInfo[i, 1].ToString() +
strStuInfo[i, 2].ToString() +
strStuInfo[i, 3].ToString() +
strStuInfo[i, 4].ToString() +
strStuInfo[i, 5].ToString());
}
}
arrAllStuInfo.Sort();
return arrAllStuInfo;
}
public ArrayList GetAllStuInfo(string strStuName)
{
ArrayList arrAllStuInfo = new ArrayList();
//遍历数组,寻找匹配的学生姓名
for (int i = 0; i < strStuInfo.GetLength(0); i++)
{
if (strStuInfo[i, 5].IndexOf(strStuName.Trim()) >= 0)
{
arrAllStuInfo.Add(strStuInfo[i, 1].ToString() +
strStuInfo[i, 2].ToString() +
strStuInfo[i, 3].ToString() +
strStuInfo[i, 4].ToString() +
strStuInfo[i, 5].ToString());
}
}
arrAllStuInfo.Sort();
return arrAllStuInfo;
}