从 Web 服务响应中删除 Xml 声明 [英] Remove Xml declaration from web service response

查看:35
本文介绍了从 Web 服务响应中删除 Xml 声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网络服务.输出的文件顶部有 XML 声明 <?xml version="1.0" encoding="utf-8"?>.我需要摆脱它才能让我的移动应用程序正常工作.我该怎么做?

I have the following web service. The output has the XML declaration <?xml version="1.0" encoding="utf-8"?> at the top of the file. I need to get rid of it for my mobile app to work correctly. How do I do it?

[WebMethod]
    public XmlDocument GetTileData(string user)
    {
        var xml = new XmlDocument();
        xml.LoadXml(string.Format(@"<tile>
                                  <visual>
                                    <binding template='TileWideSmallImageAndText02'>
                                      <image id='1' src='http://server/images/{0}_wide.png'/>
                                      <text id='1'>Custom Field : {1}/text>
                                      <text id='2'>Custom Field : {2}</text>
                                      <text id='3'>Custom Field : {3}</text>
                                    </binding>
                                    <binding template='TileSquarePeekImageAndText01'>
                                      <image id='1' src='http://server/images/{0}_square.png'/>
                                      <text id='1'>Custom Field</text>
                                      <text id='2'>{1}</text>
                                    </binding>    
                                  </visual>
                                </tile>", value1, value2, value3, value4));

        return xml;
    }

编辑 1:我尝试返回 Xml 元素而不是文档,但它也不起作用.我仍然看到声明.

Edit 1: I tried to return the Xml Element instead of the document, but it's not working either. I still see the declaration.

 [WebMethod]
        public XElement GetTileData(string user)
        {
            var xml = XDocument.Parse(string.Format(@"<tile>
                                      <visual>
                                        <binding template='TileWideSmallImageAndText02'>
                                          <image id='1' src='http://server/images/{0}_wide.png'/>
                                          <text id='1'>Custom Field : {1}/text>
                                          <text id='2'>Custom Field : {2}</text>
                                          <text id='3'>Custom Field : {3}</text>
                                        </binding>
                                        <binding template='TileSquarePeekImageAndText01'>
                                          <image id='1' src='http://server/images/{0}_square.png'/>
                                          <text id='1'>Custom Field</text>
                                          <text id='2'>{1}</text>
                                        </binding>    
                                      </visual>
                                    </tile>", value1, value2, value3, value4));

            return xml.Root;
        }

编辑 2:我能够通过使用 HttpHandler 解决这个问题.请看下面我的回答.

Edit 2: I was able to get around this issue by using a HttpHandler. See my answer below.

推荐答案

下面的方法会输出一个没有声明的对象到 XML.关键部分是 XmlWriterSettings 类.见下文.

The following method will output an object to XML without the declaration. The key part is the XmlWriterSettings class. See below.

public static string SerializeToString(object obj)
        {
            var serializer = new XmlSerializer(obj.GetType());
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            var ms = new MemoryStream();
            //the following line omits the xml declaration
            var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = new UnicodeEncoding(false, false) };
            var writer = XmlWriter.Create(ms, settings);
            serializer.Serialize(writer, obj, ns);
            return Encoding.Unicode.GetString(ms.ToArray());
        }

虽然在处理类对象时使用此方法,但在处理字符串时适用相同的原则.关键部分是 XmlWriterSettings 类,并且可能(尽管您的帖子没有提到它)是 XmlSerializerNamespaces 类.

Whereas this method is used when working with class objects, the same principals apply when working with strings. The key parts are the XmlWriterSettings class, and likely (although your post doesn't mention it) the XmlSerializerNamespaces class.

该方法将返回一个没有 xml 声明和命名空间的字符串,这对于我需要使用的 Web 服务片段来说非常完美.

The method will return a string with no xml declaration and no namespaces, which works perfectly for the web service fragments I need to use.

--编辑--

以下短程序打印没有声明和标签的所有内容:

the following short program prints everything without the declaration and without tags:

var xml = new XmlDocument();
    var fragment = @"<tile>
                          <visual>
                            <binding template='TileWideSmallImageAndText02'>
                              <image id='1' src='http://server/images/{0}_wide.png'/>
                              <text id='1'>Custom Field : {1}/text>
                              <text id='2'>Custom Field : {2}</text>
                              <text id='3'>Custom Field : {3}</text>
                            </binding>
                            <binding template='TileSquarePeekImageAndText01'>
                              <image id='1' src='http://server/images/{0}_square.png'/>
                              <text id='1'>Custom Field</text>
                              <text id='2'>{1}</text>
                            </binding>    
                          </visual>
                        </tile>";

   var returnedXml = SerializeToString(fragment);
   returnedXml = returnedXml.Replace("<string>", "");
   returnedXml = returnedXml.Replace("</string>", "");
    Console.WriteLine(returnedXml);
}

public static string SerializeToString(string obj)
{
    var serializer = new XmlSerializer(obj.GetType());
    var ns = new XmlSerializerNamespaces();
    ns.Add("", "");
    var ms = new MemoryStream();
    //the following line omits the xml declaration
    var settings = new XmlWriterSettings { OmitXmlDeclaration = true, Encoding = new UnicodeEncoding(false, false) };
    var writer = XmlWriter.Create(ms, settings);
    serializer.Serialize(writer, obj, ns);
    return Encoding.Unicode.GetString(ms.ToArray());
}

这篇关于从 Web 服务响应中删除 Xml 声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆