【发布时间】:2012-10-16 21:59:00
【问题描述】:
我有以下网络服务。输出文件顶部有 XML 声明 <?xml version="1.0" encoding="utf-8"?>。我需要摆脱它才能让我的移动应用程序正常工作。我该怎么做?
[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?
-
这是我在 Windows Metro App 部分发布的链接问题,它回答了您的问题 - stackoverflow.com/questions/12905686/…。简而言之,应用程序无法正常使用 XML 声明。
-
你能返回 XmlNode(或者实际上是 XmlElement)而不是 XmlDocument 吗?你会返回 xml.DocumentElement.
-
我刚刚尝试了您的建议(XmlNode 和 XmlElement),但我仍然在响应中看到声明。似乎是在运行时动态添加声明。
标签: c# xml web-services