【问题标题】:How to change the root tag name in xml [duplicate]如何更改xml中的根标签名称[重复]
【发布时间】: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;
        }
    }
}

【问题讨论】:

  • 您的错误在别处 - 此代码输出没有 &lt;string&gt; 标记的 xml。你是如何保存 xml 的?
  • 顺便说一句 XElement 不是 IConvertableIFormattable 所以 Convert.ToString(element) 在内部只是调用 element.ToString()
  • [点击这里] [0] 供参考:这可能会解决您的问题。 [0]:stackoverflow.com/questions/11439733/…
  • @lazyberezovsky 这是网络服务响应 xml
  • @Vignesh 创建类TramaOutput 与属性ArtistTitle。创建此类的实例并将其作为响应发送。对象将被序列化为 xml。当前您正在发送字符串,该字符串被序列化为 xml。

标签: c# asp.net xml web-services


【解决方案1】:

您的代码生成正确的xml,没有错误:

<TramaOutput>
    <Artist>bla</Artist>
    <Title>Foo</Title>
</TramaOutput>

您看到&lt;string&gt; 元素,因为您正在通过网络将此xml 作为字符串数据类型 发送。 IE。您收到带有 xml 内容的字符串。


更多示例 - 如果您发送 "42" 字符串,您会看到

<string>42</string>

如何解决您的问题?创建以下类:

public class TramaOutput
{
    public string Artist { get; set; }
    public string Title { get; set; }
}

并从您的网络服务返回它的实例:

[WebMethod]
public TramaOutput GetArtist()
{
    return new TramaOutput {Artist = "bla", Title = "foo"};
}

对象将被序列化到你的xml中:

<TramaOutput><Artist>bla</Artist><Title>foo</Title></TramaOutput>

你不需要手动构建xml!


如果你想控制序列化过程,你可以使用xml attributes。将属性应用于您的类及其成员,如下所示:

[XmlAttribute("artist")]
public string Artist { get; set; }

这会将属性序列化为属性:

<TramaOutput artist="bla"><Title>foo</Title></TramaOutput>

【讨论】:

  • 那么你的意思是return Convert.ToString(element); 创建了&lt;string&gt; 标签?
  • @MichaelPerrenoud 不,我的意思是 sending 字符串数据类型会创建 &lt;string&gt; 标签
  • 我知道了。因此,OP 向接收方发送数据的过程(即更具体地说是发送的类型)是问题所在。
  • @lazyberezovsky 我尝试了你的代码,它现在可以工作了,谢谢你的帮助。我有另一个查询,如果我想为 xml 标签使用属性,那么我需要做什么
  • @Vignesh 您可以为此使用 XmlAttribute 属性。查看更新的答案和链接
【解决方案2】:

我在.net 4.5下都检查过

Convert.ToString(element);
element.ToString();

都是返回

<TramaOutput>
    <Artist>bla</Artist>
    <Title>Foo</Title>
</TramaOutput>

您现在使用的.NET 版本和XML.Linq 版本是什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2018-11-22
    • 2020-05-05
    相关资源
    最近更新 更多