如何获得简单的c#Web服务以返回<?xml version ="1.0" encoding ="utf-16" ?>标头? [英] How can I get a simple c# web service to return <?xml version="1.0" encoding="utf-16" ?> header?
问题描述
我编写了一个非常简单的Web服务,该服务返回一个XML文档.
I've written a very simple web service that returns an XML document.
该文档的标题当前为
<?xml version="1.0" encoding="utf-8" ?>
The header for that document is currently
<?xml version="1.0" encoding="utf-8" ?>
,我希望它返回<?xml version="1.0" encoding="utf-16" ?>
如何更改.asmx或.cs文件中的默认输出编码?
How can I change the default output encoding in the .asmx or .cs file?
smo.asmx
<%@ WebService Language="C#" Class="smo" %>
smo.asmx
using <blah>
[WebService(Namespace="http://www.bl.uk/webservices/")]
public class smo : WebService
{
[XmlRoot(ElementName = "SQLServer")]
public class CDatabaseBackup
{
public string ServerName;
public string DatabaseCount;
}
//
// Generic SMO query processor
//
[WebMethod(Description = "WMIClassProperty: ", EnableSession = false, CacheDuration=60)]
public CDatabaseBackup smoDatabaseBackupStatus(string SQLServerName)
{
CDatabaseBackup result = new CDatabaseBackup();
Server svr;
<blah>
return result;
}
最终,此Web服务将在SQL Server函数中使用,并转换为xml
数据类型.根据文档,此文件必须为UTF-16
.
Ultimately this web service will be used within a SQL Server function and converted into an xml
data type. According to the documentation this needs to be UTF-16
.
alter
procedure monitor_sqlbackupaudit
as
begin
declare @l_xml_result nvarchar(max)
set @l_xml_result = ( select dbo.uspSMODatabaseBackup('sqlprod1vs') )
--set @l_xml_result = replace(@l_xml_result,'UTF-8','UTF-16');
declare @l_xml xml
set @l_xml = @l_xml_result
end
go
exec monitor_sqlbackupaudit
Msg 9402, Level 16, State 1, Procedure monitor_sqlbackupaudit, Line 15
XML parsing: line 1, character 38, unable to switch the encoding
推荐答案
您可以在Web.config的全球化"部分中将Web服务配置为处理UTF-16而不是UTF-8.全球化标签的requestEncoding和responseEncoding属性应设置为UTF-16.
You can configure your web service to process UTF-16 instead of UTF-8 in the globalization section of Web.config. The requestEncoding and responseEncoding attributes of the globalization tag should be set to UTF-16.
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
必须转换为
<globalization requestEncoding="utf-16" responseEncoding="utf-16"/>
此更改将允许Web服务输出UTF-16,但也将要求客户端在UTF-16中发出请求.
This change will allow the web service to output UTF-16, but it will also require the client to make its request in UTF-16.
这篇关于如何获得简单的c#Web服务以返回<?xml version ="1.0" encoding ="utf-16" ?>标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!