我有一个返回列表的网络服务。
[WebInvoke(Method = "POST", UriTemplate = "GetBatchList", RequestFormat = WebMessageFormat.Xml)]
public List<Batch> GetBatchList()
{
List<Batch> listBatches = null;
try
{
listBatches = api.GetBatchList();
}
catch
{
return null;
}
return listBatches;
}
其中
public class Batch
{
public string BatchName { get; set; }
public List<MAASUsers> Users { get; set; }
}
public class MAASUsers
{
public string firstName { get; set; }
public string lastName { get; set; }
public string sourceEmail { get; set; }
public int sourceTenantID { get; set; }
public string targetEmail { get; set; }
public int targetTenantID { get; set; }
public string migrationStatus { get; set; }
}
在应用程序的客户端,我无法正确获取内容。 我如何使用Web服务响应。
我试图解决这个问题:datacontractserializer deserialize list<> always empty 我还试过下面的另一个解决方案。 但是,对于这两种解决方案,我都无法检索列表:列出用户
// Call the webservice, then read the respons
[...]
using (var response = request.GetResponse())
{
DataSet dsTable = new DataSet();
dsTable.ReadXml(response.GetResponseStream(), XmlReadMode.Auto);
if (dsTable.Tables.Count > 0)
{
table = dsTable.Tables[0];
}
}
if (table.Rows.Count > 0)
{
list = (from DataRow row in table.Rows
select new Batch
{
BatchName = row["BatchName"].ToString()
/////// But here how do I get List<MAASUsers> Users?
/////// the content is returned in dsTable.Tables[1]
/////// and I don't see any "joining key" ???
}).ToList();
}
请注意,webservice已正确发送了List的内容(请参阅datacontractserializer deserialize list<> always empty以获取返回的示例XML)
答案 0 :(得分:0)
有一个关系表。数据集会自动添加它们,并为每一行添加“Class”_ID,以促进表之间的关系。 请参阅下文如何使用它。
using (var response = request.GetResponse())
{
DataSet dsTable = new DataSet();
dsTable.ReadXml(response.GetResponseStream(), XmlReadMode.Auto);
lstMaasBatches= (from DataRow row in dsTable.Tables[0].Rows
select new MAASBatch
{
BatchName = row["BatchName"].ToString(),
BatchId = Convert.ToInt32(row["MAASBatch_Id"].ToString())
}).ToList();
foreach (MAASBatch batch in lstMaasBatches)
{
int batchID = batch.BatchId;
List<MAASUsers> lst = (from DataRow row in dsTable.Tables[2].Rows
where row.Field<int>("Users_Id") == batchID
select new MAASUsers
{
ID = Convert.ToInt32(row["ID"].ToString()),
firstName = Convert.ToString(row["FirstName"].ToString()),
lastName = Convert.ToString(row["LastName"].ToString()),
migrationStatus = row["migrationStatus"].ToString(),
sourceEmail = Convert.ToString(row["SourceEmail"].ToString()),
sourceAgentID = Convert.ToInt32(row["sourceTenantID"].ToString()),
targetEmail = Convert.ToString(row["TargetEmail"].ToString()),
targetAgentID = Convert.ToInt32(row["targetTenantID"].ToString()),
}).ToList();
batch.Users = lst;
}
}
}