public class SoapAttributes
|
The members of the SoapAttributes class correspond directly to a family of attribute classes that control serialization. For example, the SoapAttributes.SoapAttribute property must be set to a SoapAttributeAttribute, which allows you to override serialization of a field or property by instructing the XmlSerializer to serialize the property value as a encoded SOAP attribute. For a complete list of attributes that control encoded SOAP serialization, see the conceptual topic at MSDN: attributesthatcontrolsoapencodedserialization.
For more details about adding an instance of the SoapAttributes class to an instance of the SoapAttributeOverrides class, see the SoapAttributeOverrides class overview.
Group
. The serialization of the
GroupName
and
IgnoreThis
fields and the members of the
GroupType
enumeration are overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; 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; // This is ignored when serialized unless it's overridden. [SoapIgnore] public bool IgnoreThis; public GroupType Grouptype; public Vehicle MyVehicle; // The SoapInclude allows the method to return a Car. [SoapInclude(typeof(Car))] public Vehicle myCar(string licNumber) { Vehicle v; if(licNumber == "") { v = new Car(); v.licenseNumber = "!!!!!!"; } else { v = new Car(); v.licenseNumber = licNumber; } return v; } } // SoapInclude allows Vehicle to accept Car type. [SoapInclude(typeof(Car))] public abstract class Vehicle { public string licenseNumber; public DateTime makeDate; } public class Car: Vehicle { } public enum GroupType { // These enums can be overridden. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("SoapOverrides.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("SoapOverrides.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); 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(); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); 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. overRideSerializer.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.IgnoreThis=true; myGroup.Grouptype= GroupType.B; Car thisCar =(Car) myGroup.myCar("1234566"); myGroup.MyVehicle=thisCar; return myGroup; } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); // 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(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); ReadGroup(myGroup); } private void ReadGroup(Group myGroup){ Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber[0]); Console.WriteLine(myGroup.GroupNumber[1]); Console.WriteLine(myGroup.Today); Console.WriteLine(myGroup.PostitiveInt); Console.WriteLine(myGroup.IgnoreThis); Console.WriteLine(); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group),soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
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 SoapAttributes class. |
ctor #2 | Overloaded:.ctor(ICustomAttributeProvider provider) Initializes a new instance of the SoapAttributes class using the specified custom type. |
SoapAttribute | Read-write Gets or sets the SoapAttributeAttribute to override. |
SoapDefaultValue | Read-write Gets or sets the default value of an XML element or attribute. |
SoapElement | Read-write Gets or sets a SoapElementAttribute to override. |
SoapEnum | Read-write Gets or sets an object that specifies how the XmlSerializer serializes a SOAP enumeration. |
SoapIgnore | Read-write Gets or sets a value that specifies whether the XmlSerializer serializes a public field or property as encoded SOAP XML. |
SoapType | Read-write Gets or sets an object that instructs the XmlSerializer how to serialize an object type into encoded SOAP XML. |
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.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
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 SoapAttributes(); |
Group
. The serialization of the
GroupName
and
IgnoreThis
fields and the members of the
GroupType
enumeration are overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; 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; // This is ignored when serialized unless it's overridden. [SoapIgnore] public bool IgnoreThis; public GroupType Grouptype; public Vehicle MyVehicle; // The SoapInclude allows the method to return a Car. [SoapInclude(typeof(Car))] public Vehicle myCar(string licNumber) { Vehicle v; if(licNumber == "") { v = new Car(); v.licenseNumber = "!!!!!!"; } else { v = new Car(); v.licenseNumber = licNumber; } return v; } } // SoapInclude allows Vehicle to accept Car type. [SoapInclude(typeof(Car))] public abstract class Vehicle { public string licenseNumber; public DateTime makeDate; } public class Car: Vehicle { } public enum GroupType { // These enums can be overridden. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("SoapOverrides.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("SoapOverrides.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); 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(); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); 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. overRideSerializer.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.IgnoreThis=true; myGroup.Grouptype= GroupType.B; Car thisCar =(Car) myGroup.myCar("1234566"); myGroup.MyVehicle=thisCar; return myGroup; } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); // 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(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); ReadGroup(myGroup); } private void ReadGroup(Group myGroup){ Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber[0]); Console.WriteLine(myGroup.GroupNumber[1]); Console.WriteLine(myGroup.Today); Console.WriteLine(myGroup.PostitiveInt); Console.WriteLine(myGroup.IgnoreThis); Console.WriteLine(); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group),soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
public SoapAttributes( |
provider
public SoapAttributeAttribute SoapAttribute {get; set;}
|
The SoapAttributes.SoapAttribute property allows you to override the serialization controlled by applying a SoapAttributeAttribute to the member. For more details on this process, see the SoapAttributeOverrides class overview.
Group
. The serialization of the
GroupName
and
IgnoreThis
fields and the members of the
GroupType
enumeration are overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; 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; // This is ignored when serialized unless it's overridden. [SoapIgnore] public bool IgnoreThis; public GroupType Grouptype; public Vehicle MyVehicle; // The SoapInclude allows the method to return a Car. [SoapInclude(typeof(Car))] public Vehicle myCar(string licNumber) { Vehicle v; if(licNumber == "") { v = new Car(); v.licenseNumber = "!!!!!!"; } else { v = new Car(); v.licenseNumber = licNumber; } return v; } } // SoapInclude allows Vehicle to accept Car type. [SoapInclude(typeof(Car))] public abstract class Vehicle { public string licenseNumber; public DateTime makeDate; } public class Car: Vehicle { } public enum GroupType { // These enums can be overridden. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("SoapOverrides.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("SoapOverrides.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); 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(); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); 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. overRideSerializer.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.IgnoreThis=true; myGroup.Grouptype= GroupType.B; Car thisCar =(Car) myGroup.myCar("1234566"); myGroup.MyVehicle=thisCar; return myGroup; } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); // 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(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); ReadGroup(myGroup); } private void ReadGroup(Group myGroup){ Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber[0]); Console.WriteLine(myGroup.GroupNumber[1]); Console.WriteLine(myGroup.Today); Console.WriteLine(myGroup.PostitiveInt); Console.WriteLine(myGroup.IgnoreThis); Console.WriteLine(); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group),soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
public object SoapDefaultValue {get; set;}
|
Group
that includes a field named
GroupName
. The default value is set with the DefaultValueAttribute to ".NET". By either not setting the field, or by setting it to ".NET", the value will not be serialized (because the default value is already known). The sample also overrides the default value in the
CreateOverrideSerializer
method, which is called by the
SerializeOverride
method. The example calls both methods,
SerializeOriginal
and
SerializeOverride
, and sets the same value (".NET") for the
GroupName
field. Because of the override, the value is serialized only when calling the
SerializeOverride
method.using System; using System.IO; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; using System.ComponentModel; public class Group { // The default is set to .NET. [DefaultValue(".NET")] public string GroupName; } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("mySoapAttributeOverridesideAttributes.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("mySoapAttributeOverridesideAttributes.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = new XmlSerializer(typeof(Group)); // 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(); // Setting the GroupName to '.NET' is like not setting it at all // because it is the default value. So no value will be // serialized, and on deserialization it will appear as a blank. myGroup.GroupName = ".NET"; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.Close(); } 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(); // The override specifies that the default value is now // 'Team1'. So setting the GroupName to '.NET' means // the value will be serialized. myGroup.GroupName = ".NET"; // Serialize the class, and close the TextWriter. overRideSerializer.Serialize(writer, myGroup); writer.Close(); } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer= new XmlSerializer(typeof(Group)); // Reading the file requires a TextReader. TextReader reader = new StreamReader(filename); // Deserialize and cast the object. Group myGroup; myGroup = (Group) mySerializer.Deserialize(reader); Console.WriteLine(myGroup.GroupName); Console.WriteLine(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires a TextReader. TextReader reader = new StreamReader(filename); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); Console.WriteLine(myGroup.GroupName); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); // Create a new DefaultValueAttribute object for the GroupName // property. DefaultValueAttribute newDefault = new DefaultValueAttribute("Team1"); soapAtts.SoapDefaultValue = newDefault; mySoapAttributeOverrides.Add(typeof(Group), "GroupName", soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
public SoapElementAttribute SoapElement {get; set;}
|
For more information, see the SoapAttributeOverrides class overview.
Transportation
. The serialization of the
Vehicle
field is overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.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 SoapEnumAttribute SoapEnum {get; set;}
|
For more information, see the SoapAttributeOverrides class overview.
Food
and
FoodType
. The
FoodType
class contains two enumerations that are overridden, and for each enumeration, the example creates a SoapEnumAttribute that it assigns to the SoapAttributes.SoapEnum property of a SoapAttributes. The example then adds the SoapAttributes to a SoapAttributeOverrides, which is used to create an XmlSerializer.using System; using System.IO; using System.Xml; using System.Xml.Serialization; public class Group{ public string GroupName; public GroupType Grouptype; } public enum GroupType{ // Use the SoapEnumAttribute to instruct the XmlSerializer // to generate Small and Large instead of A and B. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { static void Main(){ Run test= new Run(); test.SerializeObject("SoapEnum.xml"); test.SerializeOverride("SoapOverride.xml"); Console.WriteLine("Fininished writing two files"); } private void SerializeObject(string filename){ // Create an instance of the XmlSerializer Class. XmlTypeMapping mapp = (new SoapReflectionImporter()).ImportTypeMapping(typeof(Group)); XmlSerializer mySerializer = new XmlSerializer(mapp); // 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"; myGroup.Grouptype= GroupType.A; // Serialize the Class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.Close(); } private void SerializeOverride(string fileName){ SoapAttributeOverrides soapOver = new SoapAttributeOverrides(); SoapAttributes SoapAtts = new SoapAttributes(); // Add a SoapEnumAttribute for the GroupType.A enumerator. // Instead of 'A' it will be "West". SoapEnumAttribute soapEnum = new SoapEnumAttribute("West"); // Override the "A" enumerator. SoapAtts.SoapEnum = soapEnum; soapOver.Add(typeof(GroupType), "A", SoapAtts); // Add another SoapEnumAttribute for the GroupType.B enumerator. // Instead of //B// it will be "East". SoapAtts= new SoapAttributes(); soapEnum = new SoapEnumAttribute(); soapEnum.Name = "East"; SoapAtts.SoapEnum = soapEnum; soapOver.Add(typeof(GroupType), "B", SoapAtts); // Create an XmlSerializer used for overriding. XmlTypeMapping map = new SoapReflectionImporter(soapOver). ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(map); Group myGroup = new Group(); myGroup.GroupName = ".NET"; myGroup.Grouptype = GroupType.B; // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(fileName); ser.Serialize(writer, myGroup); writer.Close(); } }
public bool SoapIgnore {get; set;}
|
To override the default serialization of a field or property, create a SoapAttributes, and set its SoapAttributes.SoapIgnore property to true. Use the SoapAttributeOverrides.Add method to add the object to a SoapAttributeOverrides, specifying the type of the object that contains the field or property to ignore and the name of the field or property to ignore.
If a SoapIgnoreAttribute is applied to a field or property, the field or property will be ignored. However you can override that behavior by creating a SoapAttributes, setting its SoapAttributes.SoapIgnore property to false, and adding it to a SoapAttributeOverrides, specifying the type of the object that contains the field or property and the name of the field or property.
Group
. The serialization of the
GroupName
and
IgnoreThis
fields and the members of the
GroupType
enumeration are overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; 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; // This is ignored when serialized unless it's overridden. [SoapIgnore] public bool IgnoreThis; public GroupType Grouptype; public Vehicle MyVehicle; // The SoapInclude allows the method to return a Car. [SoapInclude(typeof(Car))] public Vehicle myCar(string licNumber) { Vehicle v; if(licNumber == "") { v = new Car(); v.licenseNumber = "!!!!!!"; } else { v = new Car(); v.licenseNumber = licNumber; } return v; } } // SoapInclude allows Vehicle to accept Car type. [SoapInclude(typeof(Car))] public abstract class Vehicle { public string licenseNumber; public DateTime makeDate; } public class Car: Vehicle { } public enum GroupType { // These enums can be overridden. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("SoapOverrides.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("SoapOverrides.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); 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(); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); 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. overRideSerializer.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.IgnoreThis=true; myGroup.Grouptype= GroupType.B; Car thisCar =(Car) myGroup.myCar("1234566"); myGroup.MyVehicle=thisCar; return myGroup; } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); // 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(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); ReadGroup(myGroup); } private void ReadGroup(Group myGroup){ Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber[0]); Console.WriteLine(myGroup.GroupNumber[1]); Console.WriteLine(myGroup.Today); Console.WriteLine(myGroup.PostitiveInt); Console.WriteLine(myGroup.IgnoreThis); Console.WriteLine(); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group),soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
public SoapTypeAttribute SoapType {get; set;}
|
Group
. The serialization of the
GroupName
and
IgnoreThis
fields and the members of the
GroupType
enumeration are overridden. In the
CreateOverrideSerializer
method, a SoapAttributeOverrides is created, and for each overridden member or enumeration, a SoapAttributes is created with the appropriate property set and added to the SoapAttributeOverrides. An XmlTypeMapping is created using the SoapAttributeOverrides, and that XmlTypeMapping is used to create the XmlSerializer that overrides the default serialization.using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Xml.Schema; 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; // This is ignored when serialized unless it's overridden. [SoapIgnore] public bool IgnoreThis; public GroupType Grouptype; public Vehicle MyVehicle; // The SoapInclude allows the method to return a Car. [SoapInclude(typeof(Car))] public Vehicle myCar(string licNumber) { Vehicle v; if(licNumber == "") { v = new Car(); v.licenseNumber = "!!!!!!"; } else { v = new Car(); v.licenseNumber = licNumber; } return v; } } // SoapInclude allows Vehicle to accept Car type. [SoapInclude(typeof(Car))] public abstract class Vehicle { public string licenseNumber; public DateTime makeDate; } public class Car: Vehicle { } public enum GroupType { // These enums can be overridden. [SoapEnum("Small")] A, [SoapEnum("Large")] B } public class Run { public static void Main() { Run test = new Run(); test.SerializeOriginal("SoapOriginal.xml"); test.SerializeOverride("SoapOverrides.xml"); test.DeserializeOriginal("SoapOriginal.xml"); test.DeserializeOverride("SoapOverrides.xml"); } public void SerializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); 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(); } public void SerializeOverride(string filename) { // Create an instance of the XmlSerializer class // that overrides the serialization. XmlSerializer overRideSerializer = CreateOverrideSerializer(); 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. overRideSerializer.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.IgnoreThis=true; myGroup.Grouptype= GroupType.B; Car thisCar =(Car) myGroup.myCar("1234566"); myGroup.MyVehicle=thisCar; return myGroup; } public void DeserializeOriginal(string filename) { // Create an instance of the XmlSerializer class. XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping( typeof(Group))); XmlSerializer mySerializer = new XmlSerializer(myMapping); // 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(); } public void DeserializeOverride(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer overRideSerializer = CreateOverrideSerializer(); // Reading the file requires an XmlTextReader. XmlTextReader reader= new XmlTextReader(filename); reader.ReadStartElement("wrapper"); // Deserialize and cast the object. Group myGroup; myGroup = (Group) overRideSerializer.Deserialize(reader); reader.ReadEndElement(); reader.Close(); ReadGroup(myGroup); } private void ReadGroup(Group myGroup){ Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber[0]); Console.WriteLine(myGroup.GroupNumber[1]); Console.WriteLine(myGroup.Today); Console.WriteLine(myGroup.PostitiveInt); Console.WriteLine(myGroup.IgnoreThis); Console.WriteLine(); } private XmlSerializer CreateOverrideSerializer() { SoapAttributeOverrides mySoapAttributeOverrides = new SoapAttributeOverrides(); SoapAttributes soapAtts = new SoapAttributes(); SoapElementAttribute mySoapElement = new SoapElementAttribute(); mySoapElement.ElementName = "xxxx"; soapAtts.SoapElement = mySoapElement; mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", soapAtts); // Override the IgnoreThis property. SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute(); soapAtts = new SoapAttributes(); soapAtts.SoapIgnore = false; mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", soapAtts); // Override the GroupType enumeration. soapAtts = new SoapAttributes(); SoapEnumAttribute xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "Over1000"; soapAtts.SoapEnum = xSoapEnum; // Add the SoapAttributes to the // mySoapAttributeOverridesrides object. mySoapAttributeOverrides.Add(typeof(GroupType), "A", soapAtts); // Create second enumeration and add it. soapAtts = new SoapAttributes(); xSoapEnum = new SoapEnumAttribute(); xSoapEnum.Name = "ZeroTo1000"; soapAtts.SoapEnum = xSoapEnum; mySoapAttributeOverrides.Add(typeof(GroupType), "B", soapAtts); // Override the Group type. soapAtts = new SoapAttributes(); SoapTypeAttribute soapType = new SoapTypeAttribute(); soapType.TypeName = "Team"; soapAtts.SoapType = soapType; mySoapAttributeOverrides.Add(typeof(Group),soapAtts); // Create an XmlTypeMapping that is used to create an instance // of the XmlSerializer. Then return the XmlSerializer object. XmlTypeMapping myMapping = (new SoapReflectionImporter( mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group)); XmlSerializer ser = new XmlSerializer(myMapping); return ser; } }
~SoapAttributes(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |