XmlSerialization of array of classes
I have a classes defined as below (generated using xsd):
[GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[SerializableAttribute]
[DebuggerStepThr开发者_如何学编程oughAttribute]
[DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(IsNullable = false)]
public class DataRequest
{
private string _serviceField;
private string _subscriptionField;
public string Service
{
get
{
return _serviceField;
}
set
{
_serviceField = value;
}
}
[XmlElement("Subscription"), DefaultValue(null)]
public string Subscription
{
get
{
return _subscriptionField;
}
set
{
_subscriptionField = value;
}
}
}
Then , I define an array of the above class:
DataRequest[] requests = new DataRequest[2];
requests[0] = new DataRequest();
requests[1] = new DataRequest();
...
I want to serialize this array and end up with the below:
<SOAP-ENV:Body>
<DataRequest>
<Service>ServiceA</DataService>
<Subscription />
</DataRequest>
<DataRequest>
<Service>ServiceB</DataService>
<Subscription />
</DataRequest>
</SOAP-ENV:Body>
However, when I call the below, where input is the array:
XmlSerializer serializer = new XmlSerializer(typeof(input));
serializer.Serialize(writer, input);
I get the following:
<Soap-ENV:Body>
<ArrayOfDataRequest>
<DataRequest>
<Service>ServiceA</DataService>
<Subscription />
</DataRequest>
<DataRequest>
<Service>ServiceB</DataService>
<Subscription />
</DataRequest>
</ArrayOfDataRequest>
</Soap-ENV:Body>
Is there some way I can get rid of the ArrayOfDataRequest element, and just have the actual array elements serialized directly?
Please correct me if i am wrong, but I think this is totally right what concerns SOAP-specific serialization. The immediate child of the body tag is the element for the parameter, in this case an array of DataRequest objects. When the serializer would leave out the ArrayOf... tags, the SOAP message would instead contain two parameters of type DataRequest.
Maybe you can specify why you would need such a "broken" message to find a solution for you actual problem?
精彩评论