去掉"ArrayOf"在c#中序列化集合时的前缀
本文关键字:quot 集合 前缀 序列化 ArrayOf 去掉 | 更新日期: 2023-09-27 18:02:26
我有一个WebMethod返回PackageItemField的集合:
[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输出。
Thanks for the help
这是你想要的吗?
[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>
要删除生成的ArrayOf,可以使用[XmlElement]