【发布时间】:2013-06-14 19:04:20
【问题描述】:
我需要将 xml 根标签名称从“string”更改为“TramaOutput”。如何实现这一点
public string ToXml()
{
XElement element = new XElement("TramaOutput",
new XElement("Artist", "bla"),
new XElement("Title", "Foo"));
return Convert.ToString(element);
}
为此,输出为:
<string>
<TramaOutput>
<Artist>bla</Artist>
<Title>Foo</Title>
</TramaOutput>
</string>
在下面提到的代码中,我收到一个错误,例如“不能在架构的顶层使用通配符”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public XElement getXl()
{
XElement element = new XElement("Root",new XElement("BookId",1),new XElement("BookId",2));
return element;
}
}
}
【问题讨论】:
-
您的错误在别处 - 此代码输出没有
<string>标记的 xml。你是如何保存 xml 的? -
顺便说一句
XElement不是IConvertable或IFormattable所以Convert.ToString(element)在内部只是调用element.ToString() -
[点击这里] [0] 供参考:这可能会解决您的问题。 [0]:stackoverflow.com/questions/11439733/…
-
@lazyberezovsky 这是网络服务响应 xml
-
@Vignesh 创建类
TramaOutput与属性Artist和Title。创建此类的实例并将其作为响应发送。对象将被序列化为 xml。当前您正在发送字符串,该字符串被序列化为 xml。
标签: c# asp.net xml web-services