2012-10-16 78 views
0

我有以下Web服务。输出在文件的顶部有XML声明<?xml version="1.0" encoding="utf-8"?>。我需要摆脱它为我的移动应用程序正常工作。我该怎么做?从Web服务响应中删除Xml声明

[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元素而不是文档,但它也不工作。我仍然看到这个声明。

[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解决此问题。请参阅下面的答案。

+1

为什么你需要删除声明? XmlDocument应该能够读取它。你如何在你的移动应用程序中使用Xml? – Pawel

+0

这是我在Windows Metro应用程序部分中发布的链接问题,它可以解答您的问题 - http://stackoverflow.com/questions/12905686/update-tile-notifcation-with-xml-returned-by-web-service。简而言之,该应用程序无法正确使用XML声明。 – tempid

+0

你可以返回XmlNode(或实际上是XmlElement)而不是XmlDocument吗?你会返回xml.DocumentElement。 – Pawel

回答

0

,而不是返回一个XmlDocument对象,返回一个字符串return xml.InnerXml;

+0

这没有奏效。我仍然可以在响应中看到声明,并将整个事件封装在一个'string'元素中。 – tempid

-1

有效的XML文档总会有这可以作为文档的第一行。没有这条线你就无法创建一个XML文档,即使你这样做了,那么这也不是一个有效的XML。

你为什么要这样做?

如果你想这样做,将其转换为字符串...在你的WebMethod中将其从XmlDocument更改为字符串...使用C#删除第一行并将其作为字符串返回。

[WebMethod] 
public string GetTileData(string user) 
{ 
    string xml = 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; 
} 

但是,我的建议是保持这第一行在那里。

+0

我需要返回XML的一个片段,而不是整个文档。我已经对该方法进行了修改(请参考我的问题中的编辑1)来返回'XElement'而不是'XDocument',但它仍然添加了声明。我的Metro应用程序需要没有声明的XML。我没有使用MVC。这是一个C#/ XAML项目。我试图将其转换为字符串(Xml.InnerXml),但该应用程序不喜欢它。它需要text/xml中的响应而不是text/plain。 – tempid

+0

在您的移动应用程序中将文本/纯文本转换为文本/ xml –

+0

恐怕Windows Metro框架不能为我提供转换响应类型的灵活性。它只需要服务的URL而没有别的。不过很好的建议! – tempid

1

以下方法将输出一个对象到XML而不用声明。关键部分是XmlWriterSettings类。见下文。

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类。

该方法将返回一个没有xml声明并且没有名称空间的字符串,它完美适用于我需要使用的Web服务片段。

- 编辑 -

以下短程序打印的一切,而不申报和无标签:

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()); 
} 
+0

当我调用它时,Web服务仍然添加XML声明。请看我回复@ AntLaC的回答。 – tempid

+0

明天我将不得不考虑它 - 我所有的代码都在工作:/ –

+0

nm,请参阅我的编辑 –

0

我能得到的输出I”通过摆脱Web服务和使用HttpHandler来寻找。这里的代码 -

 public class Handler1 : IHttpHandler 
     {   
      public void ProcessRequest(HttpContext context) 
      { 
       var xml= @"<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>"; 

       context.Response.ContentType = "text/xml"; 
       context.Response.Write(xml); 
      } 
     }