Controlling XML Serialization
For situations in which the default serialization support is not adequate, XML serialization provides mechanisms for altering the way .NET types are serialized to XML. You do this mostly by using the attributes defined in the System.Xml.Serialization namespace, which Table 7-1 describes.
You can use the XML serialization attributes only with literal, document-oriented SOAP messages. For example, the attributes will be ignored if the Web method is decorated with the SoapRpcMethod attribute or is decorated with the SoapDocumentMethod attribute and has the Use property set to SoapBinding- Use.Encoded.
The attributes listed in the table control how XML serialization represents .NET types in XML. For Web services, the attributes control how instances of .NET types are encoded into the body of a SOAP message. They also control how .NET types are represented as XML datatypes in the WSDL document that describes the Web service.
Attribute | Description |
XmlAnyAttribute | Creates an “open” XML datatype in which any attribute can be added to its root node |
XmlAnyElement | Creates an “open” XML datatype where any element can be included as a child element |
XmlArray | Controls how the root node of an array is serialized |
XmlArrayItem | Controls how an item of an array is serialized |
XmlAttribute | Indicates that a public field, property, or parameter should be serialized as an attribute |
XmlElement | Controls how a public field, property, or parameter is serialized as an element |
XmlEnum | Controls how an enumeration is serialized |
XmlIgnore | Indicates that XML serialization should not serialize the member |
XmlInclude | Tells XML serialization to include a particular datatype definition of a class that derives from a base class exposed by a Web service's interface |
XmlRoot | Identifies a type as the root of an XML document |
XmlText | Specifies that the member variable be serialized as the content of the parent element |
XmlType | Maps an XML type to a particular class, structure, enumeration, or interface declaration |
In this chapter, I use the preceding attributes to create the PurchaseOrder .NET type, which can be used to define how a purchase order is serialized in the body of a SOAP message. I also create the AcceptPO Web method. This Web method is used to receive purchase orders similar to the one in the previous example.