[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)] |
To serialize an object as an encoded SOAP message, you must construct the XmlSerializer using an XmlTypeMapping created with the SoapReflectionImporter.ImportTypeMapping method of the SoapReflectionImporter class.
Apply the SoapElementAttribute to a public field to direct the XmlSerializer to serialize the field as an encoded SOAP XML element. You can specify an alternative name for the attribute by setting the SoapAttributeAttribute.AttributeName property. Set the SoapAttributeAttribute.DataType property if the attribute must be given a specific XML schema definition (XSD) data type. If the attribute belongs to a specific XML namespace, set the SoapAttributeAttribute.Namespace property.
For more information about using attributes, see the conceptual topic at MSDN: extendingmetadatausingattributes.
Transportation
that contains a field named
Vehicle
. A SoapElementAttribute is applied to the field. When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". The
SerializeOverride
method creates a SoapElementAttribute and sets the SoapAttributes.SoapElement property of a SoapAttributes to the SoapElementAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the
Transportation
class is again serialized. Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; public class Transportation { // The SoapElementAttribute specifies that the // generated XML element name will be "Wheels" // instead of "Vehicle". [SoapElement("Wheels")] public string Vehicle; [SoapElement(DataType = "dateTime")] public DateTime CreationDate; [SoapElement(IsNullable = true)] public Thing thing; } public class Thing{ [SoapElement(IsNullable=true)] public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("SoapElementOriginal.xml"); t.SerializeOverride("SoapElementOverride.xml"); Console.WriteLine("Finished writing two XML files."); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateSoapOverrider() { // Create the SoapAttributes and SoapAttributeOverrides objects. SoapAttributes soapAttrs = new SoapAttributes(); SoapAttributeOverrides soapOverrides = new SoapAttributeOverrides(); /* Create an SoapElementAttribute to override the Vehicles property. */ SoapElementAttribute soapElement1 = new SoapElementAttribute("Truck"); // Set the SoapElement to the object. soapAttrs.SoapElement= soapElement1; /* Add the SoapAttributes to the SoapAttributeOverrides, specifying the member to override. */ soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs); // Create the XmlSerializer, and return it. XmlTypeMapping myTypeMapping = (new SoapReflectionImporter (soapOverrides)).ImportTypeMapping(typeof(Transportation)); return new XmlSerializer(myTypeMapping); } public void SerializeOverride(string filename) { // Create an XmlSerializer instance. XmlSerializer ser = CreateSoapOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate=DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } public void SerializeObject(string filename){ // Create an XmlSerializer instance. XmlSerializer ser = new XmlSerializer(typeof(Transportation)); Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate = DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the SoapElementAttribute class. |
ctor #2 | Overloaded:.ctor(string elementName) Initializes a new instance of the SoapElementAttribute class and specifies the name of the XML element. |
DataType | Read-write Gets or sets the XML Schema definition language (XSD) data type of the generated XML element. |
ElementName | Read-write Gets or sets the name of the generated XML element. |
IsNullable | Read-write Gets or sets a value indicating whether the XmlSerializer should serialize a member that is set to null into the xsi:nil attribute set to true. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public SoapElementAttribute(); |
Transportation
that contains a field named
Vehicle
. A SoapElementAttribute is applied to the field. When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". The
SerializeOverride
method creates a SoapElementAttribute and sets the SoapAttributes.SoapElement property of a SoapAttributes to the SoapElementAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the
Transportation
class is again serialized. Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; public class Transportation { // The SoapElementAttribute specifies that the // generated XML element name will be "Wheels" // instead of "Vehicle". [SoapElement("Wheels")] public string Vehicle; [SoapElement(DataType = "dateTime")] public DateTime CreationDate; [SoapElement(IsNullable = true)] public Thing thing; } public class Thing{ [SoapElement(IsNullable=true)] public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("SoapElementOriginal.xml"); t.SerializeOverride("SoapElementOverride.xml"); Console.WriteLine("Finished writing two XML files."); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateSoapOverrider() { // Create the SoapAttributes and SoapAttributeOverrides objects. SoapAttributes soapAttrs = new SoapAttributes(); SoapAttributeOverrides soapOverrides = new SoapAttributeOverrides(); /* Create an SoapElementAttribute to override the Vehicles property. */ SoapElementAttribute soapElement1 = new SoapElementAttribute("Truck"); // Set the SoapElement to the object. soapAttrs.SoapElement= soapElement1; /* Add the SoapAttributes to the SoapAttributeOverrides, specifying the member to override. */ soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs); // Create the XmlSerializer, and return it. XmlTypeMapping myTypeMapping = (new SoapReflectionImporter (soapOverrides)).ImportTypeMapping(typeof(Transportation)); return new XmlSerializer(myTypeMapping); } public void SerializeOverride(string filename) { // Create an XmlSerializer instance. XmlSerializer ser = CreateSoapOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate=DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } public void SerializeObject(string filename){ // Create an XmlSerializer instance. XmlSerializer ser = new XmlSerializer(typeof(Transportation)); Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate = DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } }
public SoapElementAttribute( |
elementName
Transportation
that contains a field named
Vehicle
. A SoapElementAttribute is applied to the field. When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". The
SerializeOverride
method creates a SoapElementAttribute and sets the SoapAttributes.SoapElement property of a SoapAttributes to the SoapElementAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the
Transportation
class is again serialized. Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; public class Transportation { // The SoapElementAttribute specifies that the // generated XML element name will be "Wheels" // instead of "Vehicle". [SoapElement("Wheels")] public string Vehicle; [SoapElement(DataType = "dateTime")] public DateTime CreationDate; [SoapElement(IsNullable = true)] public Thing thing; } public class Thing{ [SoapElement(IsNullable=true)] public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("SoapElementOriginal.xml"); t.SerializeOverride("SoapElementOverride.xml"); Console.WriteLine("Finished writing two XML files."); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateSoapOverrider() { // Create the SoapAttributes and SoapAttributeOverrides objects. SoapAttributes soapAttrs = new SoapAttributes(); SoapAttributeOverrides soapOverrides = new SoapAttributeOverrides(); /* Create an SoapElementAttribute to override the Vehicles property. */ SoapElementAttribute soapElement1 = new SoapElementAttribute("Truck"); // Set the SoapElement to the object. soapAttrs.SoapElement= soapElement1; /* Add the SoapAttributes to the SoapAttributeOverrides, specifying the member to override. */ soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs); // Create the XmlSerializer, and return it. XmlTypeMapping myTypeMapping = (new SoapReflectionImporter (soapOverrides)).ImportTypeMapping(typeof(Transportation)); return new XmlSerializer(myTypeMapping); } public void SerializeOverride(string filename) { // Create an XmlSerializer instance. XmlSerializer ser = CreateSoapOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate=DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } public void SerializeObject(string filename){ // Create an XmlSerializer instance. XmlSerializer ser = new XmlSerializer(typeof(Transportation)); Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate = DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } }
public string DataType {get; set;}
|
For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a SoapElementAttribute with the SoapAttributeAttribute.DataType set to "base64Binary" or "hexBinary", as appropriate. For the XML Schema time and date data types, use the DateTime type and apply the SoapElementAttribute with the SoapElementAttribute.DataType set to "date" or "time".
For every XML Schema data type that is mapped to a string, apply the SoapElementAttribute with its SoapElementAttribute.DataType property set to the XML Schema type. Note that this will not change the serialization format, only the schema for the member.
For more information about XML data types, see the World Wide Web Consortium (www.w3.org) document, "XML Schema Part 2: Datatypes".
XSD data type | .NET data type |
---|---|
anyURI | String |
base64Binary | Array of Byte objects |
boolean | Boolean |
byte | SByte |
date | DateTime |
dateTime | DateTime |
decimal | Decimal |
double | Double |
ENTITY | String |
ENTITIES | String |
float | Single |
gDay | String |
gMonth | String |
gMonthDay | String |
gYear | String |
gYearMonth | String |
hexBinary | Array of Byte objects |
ID | String |
IDREF | String |
IDREFS | String |
int | Int32 |
long | Int64 |
Name | String |
NCName | String |
negativeInteger | String |
NMTOKEN | String |
NMTOKENS | String |
normalizedString | String |
nonNegativeInteger | String |
nonPositiveInteger | String |
NOTATION | String |
positiveInteger | String |
QName | XmlQualifiedName |
recurringDate | String |
duration | String |
string | String |
short | Int16 |
time | DateTime |
token | String |
unsignedByte | Byte |
unsignedInt | UInt32 |
unsignedLong | UInt64 |
unsignedShort | UInt16 |
Transportation
that contains a field named
Vehicle
. A SoapElementAttribute is applied to the field. When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". The
SerializeOverride
method creates a SoapElementAttribute and sets the SoapAttributes.SoapElement property of a SoapAttributes to the SoapElementAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the
Transportation
class is again serialized. Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; public class Transportation { // The SoapElementAttribute specifies that the // generated XML element name will be "Wheels" // instead of "Vehicle". [SoapElement("Wheels")] public string Vehicle; [SoapElement(DataType = "dateTime")] public DateTime CreationDate; [SoapElement(IsNullable = true)] public Thing thing; } public class Thing{ [SoapElement(IsNullable=true)] public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("SoapElementOriginal.xml"); t.SerializeOverride("SoapElementOverride.xml"); Console.WriteLine("Finished writing two XML files."); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateSoapOverrider() { // Create the SoapAttributes and SoapAttributeOverrides objects. SoapAttributes soapAttrs = new SoapAttributes(); SoapAttributeOverrides soapOverrides = new SoapAttributeOverrides(); /* Create an SoapElementAttribute to override the Vehicles property. */ SoapElementAttribute soapElement1 = new SoapElementAttribute("Truck"); // Set the SoapElement to the object. soapAttrs.SoapElement= soapElement1; /* Add the SoapAttributes to the SoapAttributeOverrides, specifying the member to override. */ soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs); // Create the XmlSerializer, and return it. XmlTypeMapping myTypeMapping = (new SoapReflectionImporter (soapOverrides)).ImportTypeMapping(typeof(Transportation)); return new XmlSerializer(myTypeMapping); } public void SerializeOverride(string filename) { // Create an XmlSerializer instance. XmlSerializer ser = CreateSoapOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate=DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } public void SerializeObject(string filename){ // Create an XmlSerializer instance. XmlSerializer ser = new XmlSerializer(typeof(Transportation)); Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate = DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } }
public string ElementName {get; set;}
|
Transportation
that contains a field named
Vehicle
. A SoapElementAttribute is applied to the field. When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". The
SerializeOverride
method creates a SoapElementAttribute and sets the SoapAttributes.SoapElement property of a SoapAttributes to the SoapElementAttribute. The SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the
Transportation
class is again serialized. Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; public class Transportation { // The SoapElementAttribute specifies that the // generated XML element name will be "Wheels" // instead of "Vehicle". [SoapElement("Wheels")] public string Vehicle; [SoapElement(DataType = "dateTime")] public DateTime CreationDate; [SoapElement(IsNullable = true)] public Thing thing; } public class Thing{ [SoapElement(IsNullable=true)] public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("SoapElementOriginal.xml"); t.SerializeOverride("SoapElementOverride.xml"); Console.WriteLine("Finished writing two XML files."); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateSoapOverrider() { // Create the SoapAttributes and SoapAttributeOverrides objects. SoapAttributes soapAttrs = new SoapAttributes(); SoapAttributeOverrides soapOverrides = new SoapAttributeOverrides(); /* Create an SoapElementAttribute to override the Vehicles property. */ SoapElementAttribute soapElement1 = new SoapElementAttribute("Truck"); // Set the SoapElement to the object. soapAttrs.SoapElement= soapElement1; /* Add the SoapAttributes to the SoapAttributeOverrides, specifying the member to override. */ soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs); // Create the XmlSerializer, and return it. XmlTypeMapping myTypeMapping = (new SoapReflectionImporter (soapOverrides)).ImportTypeMapping(typeof(Transportation)); return new XmlSerializer(myTypeMapping); } public void SerializeOverride(string filename) { // Create an XmlSerializer instance. XmlSerializer ser = CreateSoapOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate=DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } public void SerializeObject(string filename){ // Create an XmlSerializer instance. XmlSerializer ser = new XmlSerializer(typeof(Transportation)); Transportation myTransportation = new Transportation(); myTransportation.Vehicle = "MyCar"; myTransportation.CreationDate = DateTime.Now; myTransportation.thing = new Thing(); XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, myTransportation); writer.WriteEndElement(); writer.Close(); } }
public bool IsNullable {get; set;}
|
If the SoapElementAttribute.IsNullable property is set to true, the xsi:nil attribute is generated for class members that have been set to null. For example, if you set a field named
MyStringArray
to null, the XmlSerializer generates the following XML code.
<MyStringArray xsi:nil = "true" />
If the XmlElementAttribute.IsNullable property is false, no XML element is generated.
public virtual object TypeId {get;}
|
~SoapElementAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |