简体   繁体   English

在使用WCF时,如何使用哪种数据结构返回使用FOR XML格式化的SQL Server查询结果?

[英]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.

相关问题 我在这里使用什么数据结构? - What data structure do I use here? 在等待结果时可以使用哪种结构来运行SQL Server查询并执行其他操作? - What construct to use to run a SQL Server query and do something else while waiting for the result? 如何在WCF中使用LINQ返回JSON结果? - How to return JSON result by using LINQ in WCF? SQL Server XML结果,使用C#和JavaScriptSerializer()转换为JSON? - SQL Server XML result, convert to JSON using C#, and JavaScriptSerializer()? 使用C#以编程方式导出具有数据的SQL Server表结构 - Export sql server table structure with data programmatically using C# 使用部分视图基于查询过滤数据并返回结果 - Filter data based on query and return result using Partial view 使用C#将Bulk Xml(XElement)数据插入Sql server Table的最佳方法是什么? - What is the best way to insert Bulk Xml (XElement) data to Sql server Table using C#? C#在SQL CE查询(使用WebMatrix)中返回什么数据类型? - What data type does C# return in a SQL CE query (using WebMatrix)? 使用NPOI,如何返回由Excel格式化的单元格值? - Using NPOI, how do I return a cell value as it has been formatted by Excel? 如何使用查询字符串从sql返回数据 - How can I use the Query String to return data from sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM