[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 SoapAttributeAttribute to a public field to specify that the XmlSerializer serializes the field as an XML attribute. You can specify an alternative name of the attribute by setting the SoapAttributeAttribute.AttributeName property. Set the SoapAttributeAttribute.DataType if the attribute must be given a specific XML Schema definition language (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.
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; [SoapInclude(typeof(Vehicle))] public class Group { [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; [SoapAttribute(DataType = "base64Binary")] public Byte [] GroupNumber; [SoapAttribute(DataType = "date", AttributeName = "CreationDate")] public DateTime Today; [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")] public string PostitiveInt; public Vehicle GroupVehicle; } public class Vehicle { public string licenseNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("SoapAtts.xml"); test.DeserializeObject("SoapAtts.xml"); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); Group myGroup=MakeGroup(); // Writing the file requires a TextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.WriteEndElement(); writer.Close(); } private Group MakeGroup(){ // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; Byte [] hexByte = new Byte[2]{Convert.ToByte(100), Convert.ToByte(50)}; myGroup.GroupNumber = hexByte; DateTime myDate = new DateTime(2002,5,2); myGroup.Today = myDate; myGroup.PostitiveInt= "10000"; myGroup.GroupVehicle = new Vehicle(); myGroup.GroupVehicle.licenseNumber="1234"; return myGroup; } public void DeserializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); } private XmlSerializer ReturnSOAPSerializer(){ // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping (typeof(Group))); return new XmlSerializer(myMapping); } }
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 SoapAttributeAttribute class. |
ctor #2 | Overloaded:.ctor(string attrName) Initializes a new instance of the SoapAttributeAttribute class using the specified value as the name of the XML attribute. |
AttributeName | Read-write Gets or sets the name of the XML attribute generated by the XmlSerializer. |
DataType | Read-write Gets or sets the XML Schema definition language (XSD) data type of the SOAP attribute generated by the XmlSerializer. |
Namespace | Read-write Gets or sets the XML namespace of the XML attribute. |
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 SoapAttributeAttribute(); |
using System; using System.IO; using System.Xml; using System.Xml.Serialization; public class Group { // This attribute will be overridden. [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; } public class Run { public static void Main() { Run test = new Run(); test.SerializeOverride("SoapOveride.xml"); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; // Serialize the class, and close the TextWriter. overRideSerializer.Serialize(writer, myGroup); writer.Close(); } private XmlSerializer CreateOverrideSerializer(){ SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes mySoapAttributes = new SoapAttributes(); // Create a new SoapAttributeAttribute to override the // one applied to the Group class. The resulting XML // stream will use the new namespace and attribute name. SoapAttributeAttribute mySoapAttribute = new SoapAttributeAttribute(); mySoapAttribute.AttributeName = "TeamName"; // Change the Namespace. mySoapAttribute.Namespace = "http://www.cohowinery.com"; mySoapAttributes.SoapAttribute = mySoapAttribute; mySoapAttributeOverrides. Add(typeof(Group), "GroupName" ,mySoapAttributes); XmlTypeMapping myMapping = (new SoapReflectionImporter (mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } } //<?xml version="1.0" encoding="utf-8" ?> // <Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //xmlns:xsd="http://www.w3.org/2001/XMLSchema" n1:TeamName=".NET" //xmlns:n1="http://www.cohowinery" />
public SoapAttributeAttribute( |
attrName
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; [SoapInclude(typeof(Vehicle))] public class Group { [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; [SoapAttribute(DataType = "base64Binary")] public Byte [] GroupNumber; [SoapAttribute(DataType = "date", AttributeName = "CreationDate")] public DateTime Today; [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")] public string PostitiveInt; public Vehicle GroupVehicle; } public class Vehicle { public string licenseNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("SoapAtts.xml"); test.DeserializeObject("SoapAtts.xml"); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); Group myGroup=MakeGroup(); // Writing the file requires a TextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.WriteEndElement(); writer.Close(); } private Group MakeGroup(){ // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; Byte [] hexByte = new Byte[2]{Convert.ToByte(100), Convert.ToByte(50)}; myGroup.GroupNumber = hexByte; DateTime myDate = new DateTime(2002,5,2); myGroup.Today = myDate; myGroup.PostitiveInt= "10000"; myGroup.GroupVehicle = new Vehicle(); myGroup.GroupVehicle.licenseNumber="1234"; return myGroup; } public void DeserializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); } private XmlSerializer ReturnSOAPSerializer(){ // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping (typeof(Group))); return new XmlSerializer(myMapping); } }
public string AttributeName {get; set;}
|
Today
field.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; [SoapInclude(typeof(Vehicle))] public class Group { [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; [SoapAttribute(DataType = "base64Binary")] public Byte [] GroupNumber; [SoapAttribute(DataType = "date", AttributeName = "CreationDate")] public DateTime Today; [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")] public string PostitiveInt; public Vehicle GroupVehicle; } public class Vehicle { public string licenseNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("SoapAtts.xml"); test.DeserializeObject("SoapAtts.xml"); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); Group myGroup=MakeGroup(); // Writing the file requires a TextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.WriteEndElement(); writer.Close(); } private Group MakeGroup(){ // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; Byte [] hexByte = new Byte[2]{Convert.ToByte(100), Convert.ToByte(50)}; myGroup.GroupNumber = hexByte; DateTime myDate = new DateTime(2002,5,2); myGroup.Today = myDate; myGroup.PostitiveInt= "10000"; myGroup.GroupVehicle = new Vehicle(); myGroup.GroupVehicle.licenseNumber="1234"; return myGroup; } public void DeserializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); } private XmlSerializer ReturnSOAPSerializer(){ // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping (typeof(Group))); return new XmlSerializer(myMapping); } }
public string DataType {get; set;}
|
Exception Type | Condition |
---|---|
Exception | The XML Schema data type you've specified cannot be mapped to the .NET data type. |
For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a SoapAttributeAttribute 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 SoapAttributeAttribute with the SoapAttributeAttribute.DataType set to "date" or "time".
For every XML Schema data type that is mapped to a string, apply the SoapAttributeAttribute with its SoapAttributeAttribute.DataType property set to the XML Schema data type. Note that this will not change the serialization format, only the schema for the member.
For more information about XML Schema data types, see the World Wide Consortium (www.w3.org ) document named "XML Schema Part 2: Datatypes".
XML Schema 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 |
integer | String |
language | String |
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 |
GroupNumber
and the
Today
fields.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; [SoapInclude(typeof(Vehicle))] public class Group { [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; [SoapAttribute(DataType = "base64Binary")] public Byte [] GroupNumber; [SoapAttribute(DataType = "date", AttributeName = "CreationDate")] public DateTime Today; [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")] public string PostitiveInt; public Vehicle GroupVehicle; } public class Vehicle { public string licenseNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("SoapAtts.xml"); test.DeserializeObject("SoapAtts.xml"); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); Group myGroup=MakeGroup(); // Writing the file requires a TextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.WriteEndElement(); writer.Close(); } private Group MakeGroup(){ // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; Byte [] hexByte = new Byte[2]{Convert.ToByte(100), Convert.ToByte(50)}; myGroup.GroupNumber = hexByte; DateTime myDate = new DateTime(2002,5,2); myGroup.Today = myDate; myGroup.PostitiveInt= "10000"; myGroup.GroupVehicle = new Vehicle(); myGroup.GroupVehicle.licenseNumber="1234"; return myGroup; } public void DeserializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); } private XmlSerializer ReturnSOAPSerializer(){ // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping (typeof(Group))); return new XmlSerializer(myMapping); } }
public string Namespace {get; set;}
|
To create namespaces that are associated with prefixes, you must create an XmlSerializerNamespaces that contains the namespaces and prefixes used in the XML document. The namespace you set for each XmlAttributeAttribute must match one of the namespaces in the XmlSerializerNamespaces. When the XmlSerializer generates the XML code, it correctly prefixes each attribute name.
GroupName
the field.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; [SoapInclude(typeof(Vehicle))] public class Group { [SoapAttribute (Namespace = "http://www.cpandl.com")] public string GroupName; [SoapAttribute(DataType = "base64Binary")] public Byte [] GroupNumber; [SoapAttribute(DataType = "date", AttributeName = "CreationDate")] public DateTime Today; [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")] public string PostitiveInt; public Vehicle GroupVehicle; } public class Vehicle { public string licenseNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("SoapAtts.xml"); test.DeserializeObject("SoapAtts.xml"); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); Group myGroup=MakeGroup(); // Writing the file requires a TextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.WriteEndElement(); writer.Close(); } private Group MakeGroup(){ // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. myGroup.GroupName = ".NET"; Byte [] hexByte = new Byte[2]{Convert.ToByte(100), Convert.ToByte(50)}; myGroup.GroupNumber = hexByte; DateTime myDate = new DateTime(2002,5,2); myGroup.Today = myDate; myGroup.PostitiveInt= "10000"; myGroup.GroupVehicle = new Vehicle(); myGroup.GroupVehicle.licenseNumber="1234"; return myGroup; } public void DeserializeObject(string filename) { // Create an instance of the XmlSerializer class that // can generate encoded SOAP messages. XmlSerializer mySerializer = ReturnSOAPSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); } private XmlSerializer ReturnSOAPSerializer(){ // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping (typeof(Group))); return new XmlSerializer(myMapping); } }
public virtual object TypeId {get;}
|
~SoapAttributeAttribute(); |
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(); |