[英]In using WCF, What data structure do I use to return a SQL Server query result that is formatted using FOR XML?
I'm using WCF to create a service that will query my SQL Server DB and return the response in a specific XML layout (shown here) 我正在使用WCF创建一个服务,该服务将查询我的SQL Server数据库并以特定的XML布局返回响应(如下所示)
<SUP_LOCS>
<SUP_LOC>
<SUP_LOC_ID_DESC>Memphis TN</SUP_LOC_ID_DESC>
<QTY_AVL>6</QTY_AVL>
<ITEM_UOM>EA</ITEM_UOM>
</SUP_LOC>
<SUP_LOC>
<SUP_LOC_ID_DESC>Los Angeles CA</SUP_LOC_ID_DESC>
<QTY_AVL>1</QTY_AVL>
<ITEM_UOM>EA</ITEM_UOM>
</SUP_LOC>
</SUP_LOCS>
I am using FOR XML to make SQL Server produce the XML. 我正在使用FOR XML使SQL Server生成XML。 This gives me the correct XML format I show above.
这给了我上面显示的正确XML格式。
select location [Sup_LOC_ID_DESC] ...(shortened for brevity)... FOR XML raw('SUP_LOC'), root('SUP_LOCS'), elements
My question is what data structure should I use in my method? 我的问题是我的方法应该使用哪种数据结构? (shown here).
(如此处所示)。 In my example, I am using string but when I use the WCF test client, my response doesn't look like I thought it would (see image below)
在我的示例中,我使用的是字符串,但是当我使用WCF测试客户端时,我的响应看起来不像我想的那样(请参见下图)
public string GetInventory(string itemID)
{
inventoryQuery = string.Format(inventoryQuery, itemID);
string queryResult = _sub.Database.SqlQuery<string>(inventoryQuery).FirstOrDefault();
return queryResult;
}
Your return type can be System.ServiceModel.Channels.Message
and you can create a message using WebOperationContext.Current.CreateTextResponse
method 您的返回类型可以是
System.ServiceModel.Channels.Message
并且可以使用WebOperationContext.Current.CreateTextResponse
方法创建消息。
So, your method can be like 因此,您的方法可以像
public System.ServiceModel.Channels.Message GetInventory(string itemID)
{
inventoryQuery = string.Format(inventoryQuery, itemID);
string queryResult = _sub.Database.SqlQuery<string>(inventoryQuery).FirstOrDefault();
return WebOperationContext.Current.CreateTextResponse(queryResult,"text/xml");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.