我有以下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解决此问题。请参阅下面的答案。
为什么你需要删除声明? XmlDocument应该能够读取它。你如何在你的移动应用程序中使用Xml? – Pawel
这是我在Windows Metro应用程序部分中发布的链接问题,它可以解答您的问题 - http://stackoverflow.com/questions/12905686/update-tile-notifcation-with-xml-returned-by-web-service。简而言之,该应用程序无法正确使用XML声明。 – tempid
你可以返回XmlNode(或实际上是XmlElement)而不是XmlDocument吗?你会返回xml.DocumentElement。 – Pawel