我有一个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输出。
感谢您的帮助
答案 0 :(得分:1)
这更像是你的想法吗?
[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>
答案 1 :(得分:0)
要删除生成的ArrayOf,您可以使用[XmlElement]