c# - 在 C# 中序列化集合时摆脱 "ArrayOf"前缀

标签 c#

我有一个返回 PackageItemField 集合的 WebMethod:

[WebMethod]        
public List<PackageItemField> GetAvailablePackges()
{
    ... ;
}

我得到的:

<ArrayOfPackageItemField xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <PackageItemField name="a">1</PackageItemField>
  <PackageItemField name="b">2</PackageItemField>
</ArrayOfPackageItemField>

我需要的是:个性化根元素名称

<SomeThingElse>
  <PackageItemField name="a">1</PackageItemField>
  <PackageItemField name="b">2</PackageItemField>
</SomeThingElse>

不适合我的解决方案: 将集合作为属性封装在一个新类中并使用以下属性

public class ClassTest
{
    private List<PackageItemField> coll;

    [XmlArray("SomeThingElse")]
    [XmlArrayItem("PackageItemField")]
    public List<PackageItemField> Coll
    {
        get
        {
            return coll;
        }
        set
        {
            coll = value;
        }
    }
}

为什么不: 因为我将有一个根节点作为 ClassTest int 输出。

感谢帮助

最佳答案

这更符合您的想法吗?

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class TestService : System.Web.Services.WebService
{
    [WebMethod]
    [return: XmlArray("MyArray")]
    [return: XmlArrayItem("MyItem")]
    public List<string> HelloWorld()
    {
        return new List<string>() { "Hello World" };
    }
}

SOAP 1.2 响应:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <MyArray>
        <MyItem>string</MyItem>
        <MyItem>string</MyItem>
      </MyArray>
    </HelloWorldResponse>
  </soap12:Body>
</soap12:Envelope>

关于c# - 在 C# 中序列化集合时摆脱 "ArrayOf"前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749092/

相关文章:

c# - 使用查询字符串在 MVC C# 中使用操作过滤器

c# - 合并的 ObservableCollection

C# - Websphere MQ 版本 7.0.1 的 SSL

c# - 应该如何在 ASP.Net 中对事件进行单元测试?

c# - MongoDB。索引超出范围

C# 反序列化列表计数为零

c# - 如何在 Windows 8 中使用 StreamWriter 写入文件?

c# - 没有分配给 var 的 COM 对象是否没有被释放?

c# - TemplateBinding 不适用于文本框文本

c# - 通过 LINQ 递归选择?