我生成一个简单的Web服务。在其中,当我呼叫服务时,输出将以不同方式显示。代码是......
public class DistictName
{
public string DisticName { get; set; }
}
public class Distict
{
public string DistictName { get; set; }
public List<DistictName> DistName { get; set; }
}
[WebMethod]
public Distict DistictNameByState(String StateName)
{
Distict DName = new Distict();
DName.DistName = new List<DistictName>();
DataTable dtDistictName = clsDefault.getproduct("sp_getDistictName", "Distict", StateName.ToString());
for (int i = 0; i < dtDistictName.Rows.Count; i++)
{
DName.DistName.Add(new DistictName() { DisticName = dtDistictName.Rows[i]["Distict_Name"].ToString() });
}
return DName;
}
当我输入状态名称例如Gujarat时,区域名称将显示在SoapUI编辑器上,如...
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<DistictNameByStateResponse xmlns="http://tempuri.org/">
<DistictNameByStateResult>
<DistName>
<DistictName>
<DisticName>Ahmedabad</DisticName>
</DistictName>
<DistictName>
<DisticName>Amreli</DisticName>
</DistictName>
<DistictName>
<DisticName>Anand</DisticName>
</DistictName>
<DistictName>
<DisticName>Aravalli</DisticName>
</DistictName>
<DistictName>
<DisticName>Banaskantha</DisticName>
</DistictName>
<DistictName>
<DisticName>Bharuch</DisticName>
</DistictName>
<DistictName>
<DisticName>Bhavnagar</DisticName>
</DistictName>
</DistName>
</DistictNameByStateResult>
</DistictNameByStateResponse>
</soap:Body>
</soap:Envelope>
但我需要这种类型的输出......
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<DistictNameByStateResponse xmlns="http://tempuri.org/">
<DistictNameByStateResult>
<DistName>
<DisticName>Ahmedabad</DisticName>
<DisticName>Amreli</DisticName>
<DisticName>Anand</DisticName>
<DisticName>Aravalli</DisticName>
<DisticName>Banaskantha</DisticName>
<DisticName>Bharuch</DisticName>
<DisticName>Bhavnagar</DisticName>
</DistName>
</DistictNameByStateResult>
</DistictNameByStateResponse>
</soap:Body>
</soap:Envelope>
怎么做?我能在代码中改变什么......?
答案 0 :(得分:0)
您需要返回List<Distict>
。所以你需要使用folloiwng代码。
public class Distict
{
public string DistictName { get; set; }
}
[WebMethod]
public List<Distict> DistictNameByState(String StateName)
{
List<Distict> DName = new List<Distict>();
DataTable dtDistictName = clsDefault.getproduct("sp_getDistictName", "Distict", StateName.ToString());
for (int i = 0; i < dtDistictName.Rows.Count; i++)
{
Distict dst = new Distict();
dst.DisticName = dtDistictName.Rows[i]["Distict_Name"].ToString() ;
DName.Add(dst);
}
return DName;
}