我有一个返回 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/