标题?" />

如何获得一个简单的 C# Web 服务来返回 标题?

发布于 2024-10-01 18:31:32 字数 1520 浏览 4 评论 0 原文

我编写了一个非常简单的 Web 服务,它返回 XML 文档。

该文档的标题当前是

我希望它返回

如何更改 .asmx 或 .cs 文件中的默认输出编码?

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

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

I've written a very simple web service that returns an XML document.

The header for that document is currently
<?xml version="1.0" encoding="utf-8" ?>

and I would like it to return <?xml version="1.0" encoding="utf-16" ?>

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;
      }

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-10-08 18:31:32

您可以在 Web.config 的全球化部分中将 Web 服务配置为处理 UTF-16 而不是 UTF-8。全球化标记的 requestEncoding 和 responseEncoding 属性应设置为 UTF-16。

<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

必须转换为

<globalization requestEncoding="utf-16" responseEncoding="utf-16"/>

此更改将允许 Web 服务输出 UTF-16,但也要求客户端以 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"/>

must be converted to

<globalization requestEncoding="utf-16" responseEncoding="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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文