SoapAttributeOverrides Class
Assembly: System.Xml (in system.xml.dll)
The SoapAttributeOverrides class enables an XmlSerializer to override the default way of serializing a set of objects. Overriding serialization in this way has two uses: first, you can control and augment the serialization of objects found in a DLL, even if you do not have access to the source; second, you can create one set of serializable classes, but serialize the objects in multiple ways. For example, instead of serializing members of a class instance as XML elements, you can serialize them as XML attributes, resulting in a more efficient document to transport.
After you create a SoapAttributeOverrides, you create an XmlTypeMapping using the ImportTypeMapping method of the SoapReflectionImporter class. Pass the resulting object as an argument to the XmlSerializer constructor. The resulting XmlSerializer uses the data contained by the SoapAttributeOverrides to override attributes that control how objects are serialized. To accomplish this, the SoapAttributeOverrides contains a collection of the object types that are overridden, as well as a SoapAttributes associated with each overridden object type. Each SoapAttributes contains an appropriate set of attribute objects that control how each field, property, or class is serialized.
The process for creating and using a SoapAttributeOverrides is as follows:
-
Create a SoapAttributes.
-
Create an attribute object that is appropriate to the object being overridden. For example, to override a field or property, create a SoapElementAttribute, using the new, derived type. You can optionally assign a new ElementName that overrides the base class's attribute name or namespace.
-
Add the attribute object to the appropriate SoapAttributes property or collection. For example, you would set the SoapElement property of the SoapAttributes object to the SoapElementAttribute and specify the member name that is being overridden.
-
Create a SoapAttributeOverrides.
-
Add the SoapAttributes to the SoapAttributeOverrides using the Add method. If the object being overridden is a SoapTypeAttribute, you need only specify the type of the overridden object. But if you are overriding a field or property, you must also specify the name of the overridden member.
-
Create an XmlTypeMapping using the ImportTypeMapping method of the SoapReflectionImporter class.
-
When constructing the XmlSerializer, pass the XmlTypeMapping to the XmlSerializer constructor.
-
Use the resulting XmlSerializer to serialize or deserialize the class objects.
The following example serializes a class named 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.
Imports System Imports System.IO Imports System.Text Imports System.Xml Imports System.Xml.Serialization Imports System.Xml.Schema Public Class Group <SoapAttribute (Namespace:= "http:'www.cpandl.com")> _ Public GroupName As String <SoapAttribute(DataType:= "base64Binary")> _ Public GroupNumber() As Byte <SoapAttribute(DataType:= "date", _ AttributeName:= "CreationDate")> _ Public Today As DateTime <SoapElement(DataType:= "nonNegativeInteger", _ ElementName:= "PosInt")> _ Public PostitiveInt As String ' This is ignored when serialized unless it's overridden. <SoapIgnore> _ Public IgnoreThis As Boolean Public Grouptype As GroupType Public MyVehicle As Vehicle ' The SoapInclude allows the method to return a Car. <SoapInclude(GetType(Car))> _ Public Function myCar(licNumber As String ) As Vehicle Dim v As Vehicle if licNumber = "" Then v = New Car() v.licenseNumber = "!!!!!!" else v = New Car() v.licenseNumber = licNumber End If return v End Function End Class ' SoapInclude allows Vehicle to accept Car type. <SoapInclude(GetType(Car))> _ Public MustInherit class Vehicle Public licenseNumber As String Public makeDate As DateTime End Class Public Class Car Inherits Vehicle End Class Public enum GroupType ' These enums can be overridden. <SoapEnum("Small")> _ A <SoapEnum("Large")> _ B End Enum Public Class Run Shared Sub Main() Dim test As Run = New Run() test.SerializeOriginal("SoapOriginal.xml") test.SerializeOverride("SoapOverrides.xml") test.DeserializeOriginal("SoapOriginal.xml") test.DeserializeOverride("SoapOverrides.xml") End SUb Public Sub SerializeOriginal(filename As String) ' Create an instance of the XmlSerializer class. Dim myMapping As XmlTypeMapping = _ (New SoapReflectionImporter().ImportTypeMapping _ (GetType(Group))) Dim mySerializer As XmlSerializer = _ New XmlSerializer(myMapping) Dim myGroup As Group =MakeGroup() ' Writing the file requires a TextWriter. Dim writer As XmlTextWriter = _ 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() End Sub Public Sub SerializeOverride(filename As String) ' Create an instance of the XmlSerializer class ' that overrides the serialization. Dim overRideSerializer As XmlSerializer = _ CreateOverrideSerializer() Dim myGroup As Group =MakeGroup() ' Writing the file requires a TextWriter. Dim writer As XmlTextWriter = _ 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() End Sub private Function MakeGroup() As Group ' Create an instance of the class that will be serialized. Dim myGroup As Group = New Group() ' Set the object properties. myGroup.GroupName = ".NET" Dim hexByte()As Byte = new Byte(1){Convert.ToByte(100), _ Convert.ToByte(50)} myGroup.GroupNumber = hexByte Dim myDate As DateTime = new DateTime(2002,5,2) myGroup.Today = myDate myGroup.PostitiveInt = "10000" myGroup.IgnoreThis = true myGroup.Grouptype = GroupType.B Dim thisCar As Car thisCar =CType(myGroup.myCar("1234566"), Car) myGroup.myVehicle=thisCar return myGroup End Function Public Sub DeserializeOriginal(filename As String) ' Create an instance of the XmlSerializer class. Dim myMapping As XmlTypeMapping = _ (New SoapReflectionImporter().ImportTypeMapping _ (GetType(Group))) Dim mySerializer As XmlSerializer = _ New XmlSerializer(myMapping) ' Reading the file requires an XmlTextReader. Dim reader As XmlTextReader = _ New XmlTextReader(filename) reader.ReadStartElement("wrapper") ' Deserialize and cast the object. Dim myGroup As Group = _ CType(mySerializer.Deserialize(reader), Group) reader.ReadEndElement() reader.Close() End Sub Public Sub DeserializeOverride(filename As String) ' Create an instance of the XmlSerializer class. Dim overRideSerializer As XmlSerializer = _ CreateOverrideSerializer() ' Reading the file requires an XmlTextReader. Dim reader As XmlTextReader = _ New XmlTextReader(filename) reader.ReadStartElement("wrapper") ' Deserialize and cast the object. Dim myGroup As Group = _ CType(overRideSerializer.Deserialize(reader), Group) reader.ReadEndElement() reader.Close() ReadGroup(myGroup) End Sub private Sub ReadGroup(myGroup As Group) 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() End Sub Private Function CreateOverrideSerializer() As XmlSerializer Dim soapOver As SoapAttributeOverrides = New SoapAttributeOverrides() Dim soapAtts As SoapAttributes = New SoapAttributes() Dim mySoapElement As SoapElementAttribute = New SoapElementAttribute() mySoapElement.ElementName = "xxxx" soapAtts.SoapElement = mySoapElement soapOver.Add(GetType(Group), "PostitiveInt", soapAtts) ' Override the IgnoreThis property. Dim myIgnore As SoapIgnoreAttribute = new SoapIgnoreAttribute() soapAtts = New SoapAttributes() soapAtts.SoapIgnore = false soapOver.Add(GetType(Group), "IgnoreThis", soapAtts) ' Override the GroupType enumeration. soapAtts = New SoapAttributes() Dim xSoapEnum As SoapEnumAttribute = new SoapEnumAttribute() xSoapEnum.Name = "Over1000" soapAtts.SoapEnum = xSoapEnum ' Add the SoapAttributes to the SoapOverrides object. soapOver.Add(GetType(GroupType), "A", soapAtts) ' Create second enumeration and add it. soapAtts = New SoapAttributes() xSoapEnum = New SoapEnumAttribute() xSoapEnum.Name = "ZeroTo1000" soapAtts.SoapEnum = xSoapEnum soapOver.Add(GetType(GroupType), "B", soapAtts) ' Override the Group type. soapAtts = New SoapAttributes() Dim soapType As SoapTypeAttribute = New SoapTypeAttribute() soapType.TypeName = "Team" soapAtts.SoapType = soapType soapOver.Add(GetType(Group),soapAtts) Dim myMapping As XmlTypeMapping = (New SoapReflectionImporter( _ soapOver)).ImportTypeMapping(GetType(Group)) Dim ser As XmlSerializer = new XmlSerializer(myMapping) return ser End Function End Class
import System.*;
import System.IO.*;
import System.Text.*;
import System.Xml.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
public class Group
{
/** @attribute SoapAttribute(Namespace = "http://www.cpandl.com")
*/
public String groupName;
/** @attribute SoapAttribute(DataType = "base64Binary")
*/
public System.Byte groupNumber[];
/** @attribute SoapAttribute(DataType = "date",
AttributeName = "CreationDate")
*/
public DateTime today;
/** @attribute SoapElement(DataType = "nonNegativeInteger",
ElementName = "PosInt")
*/
public String postitiveInt;
// This is ignored when serialized unless it's overridden.
/** @attribute SoapIgnore()
*/
public boolean ignoreThis;
public GroupType groupType;
public Vehicle myVehicle;
// The SoapInclude allows the method to return a Car.
/** @attribute SoapInclude(Car.class)
*/
public Vehicle MyCar(String licNumber)
{
Vehicle v;
if (licNumber.Equals("")) {
v = new Car();
v.licenseNumber = "!!!!!!";
}
else {
v = new Car();
v.licenseNumber = licNumber;
}
return v;
} //MyCar
} //Group
// SoapInclude allows Vehicle to accept Car type.
/** @attribute SoapInclude(Car.class)
*/
abstract public class Vehicle
{
public String licenseNumber;
public DateTime makeDate;
} //Vehicle
public class Car extends Vehicle
{
} //Car
public class GroupType
{
public int member;
public GroupType()
{
member = 0;
} //GroupType
public GroupType(int n)
{
member = n;
} //GroupType
/** @attribute SoapEnum("Small")
*/
public static int a = 0;
/** @attribute SoapEnum("Large")
*/
public static int b = 1;
} //GroupType
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeOriginal("SoapOriginal.xml");
test.SerializeOverride("SoapOverrides.xml");
test.DeserializeOriginal("SoapOriginal.xml");
test.DeserializeOverride("SoapOverrides.xml");
} //main
public void SerializeOriginal(String fileName)
{
// Create an instance of the XmlSerializer class.
XmlTypeMapping myMapping = (new SoapReflectionImporter()).
ImportTypeMapping(Group.class.ToType());
XmlSerializer mySerializer = new XmlSerializer(myMapping);
Group myGroup = MakeGroup();
// Writing the file requires a TextWriter.
XmlTextWriter writer = new XmlTextWriter(fileName,
Encoding.get_UTF8());
writer.set_Formatting(Formatting.Indented);
writer.WriteStartElement("wrapper");
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.WriteEndElement();
writer.Close();
} //SerializeOriginal
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.get_UTF8());
writer.set_Formatting(Formatting.Indented);
writer.WriteStartElement("wrapper");
// Serialize the class, and close the TextWriter.
overRideSerializer.Serialize(writer, myGroup);
writer.WriteEndElement();
writer.Close();
} //SerializeOverride
private Group MakeGroup()
{
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.groupName = ".NET";
System.Byte hexByte[] = new System.Byte[] { (System.Byte)100,
(System.Byte)50 };
myGroup.groupNumber = hexByte;
DateTime myDate = new DateTime(2002, 5, 2);
myGroup.today = myDate;
myGroup.postitiveInt = "10000";
myGroup.ignoreThis = true;
myGroup.groupType = new GroupType(GroupType.b);
Car thisCar = (Car)myGroup.MyCar("1234566");
myGroup.myVehicle = thisCar;
return myGroup;
} //MakeGroup
public void DeserializeOriginal(String fileName)
{
// Create an instance of the XmlSerializer class.
XmlTypeMapping myMapping = (new SoapReflectionImporter()).
ImportTypeMapping(Group.class.ToType());
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();
} //DeserializeOriginal
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);
} //DeserializeOverride
private void ReadGroup(Group myGroup)
{
Console.WriteLine(myGroup.groupName);
Console.WriteLine(myGroup.groupNumber.get_Item(0));
Console.WriteLine(myGroup.groupNumber.get_Item(1));
Console.WriteLine(myGroup.today);
Console.WriteLine(myGroup.postitiveInt);
Console.WriteLine(myGroup.ignoreThis);
Console.WriteLine();
} //ReadGroup
private XmlSerializer CreateOverrideSerializer()
{
SoapAttributeOverrides mySoapAttributeOverrides
= new SoapAttributeOverrides();
SoapAttributes soapAtts = new SoapAttributes();
SoapElementAttribute mySoapElement = new SoapElementAttribute();
mySoapElement.set_ElementName("xxxx");
soapAtts.set_SoapElement(mySoapElement);
mySoapAttributeOverrides.Add(Group.class.ToType(), "postitiveInt",
soapAtts);
// Override the ignoreThis property.
SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute();
soapAtts = new SoapAttributes();
soapAtts.set_SoapIgnore(false);
mySoapAttributeOverrides.Add(Group.class.ToType(), "ignoreThis",
soapAtts);
// Override the GroupType enumeration.
soapAtts = new SoapAttributes();
SoapEnumAttribute xSoapEnum = new SoapEnumAttribute();
xSoapEnum.set_Name("Over1000");
soapAtts.set_SoapEnum(xSoapEnum);
// Add the SoapAttributes to the
// mySoapAttributeOverridesrides object.
mySoapAttributeOverrides.Add(GroupType.class.ToType(), "a", soapAtts);
// Create second enumeration and add it.
soapAtts = new SoapAttributes();
xSoapEnum = new SoapEnumAttribute();
xSoapEnum.set_Name("ZeroTo1000");
soapAtts.set_SoapEnum(xSoapEnum);
mySoapAttributeOverrides.Add(GroupType.class.ToType(), "b", soapAtts);
// Override the Group type.
soapAtts = new SoapAttributes();
SoapTypeAttribute soapType = new SoapTypeAttribute();
soapType.set_TypeName("Team");
soapAtts.set_SoapType(soapType);
mySoapAttributeOverrides.Add(Group.class.ToType(), 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(Group.class.ToType());
XmlSerializer ser = new XmlSerializer(myMapping);
return ser;
} //CreateOverrideSerializer
} //Run
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.