public class XmlTypeMapping : XmlMapping
|
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(); } }
ElementName | Read-only Gets the XML element name of the mapped object. |
Namespace | Read-only Gets the XML namespace of the mapped object. |
TypeFullName | Read-only The fully qualified type name, including the namespace (or namespaces) and type. |
TypeName | Read-only Gets the type name of the mapped object. |
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 string ElementName {get;}
|
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; namespace Company{ [SoapType("TheGroup", "http://www.cohowinery.com")] public class Group { public string GroupName; public Thing[] Things; [SoapElement(DataType = "language")] public string Lang = "en"; [SoapElement(DataType = "integer")] public string MyNumber; [SoapElement(DataType = "duration")] public string ReDate = "8/31/01"; } public class Thing{ public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.GetMap("MyMap.xml"); } public void GetMap(string filename){ // Create an XmlSerializer instance. XmlTypeMapping map = new SoapReflectionImporter().ImportTypeMapping(typeof(Group)); Console.WriteLine("ElementName: " + map.ElementName); Console.WriteLine("Namespace: " + map.Namespace); Console.WriteLine("TypeFullName: " + map.TypeFullName); Console.WriteLine("TypeName: " + map.TypeName); XmlSerializer ser = new XmlSerializer(map); Group xGroup= new Group(); xGroup.GroupName= "MyCar"; xGroup.MyNumber= 5454.ToString(); xGroup.Things = new Thing[]{new Thing(), new Thing()}; // To write the outer wrapper, use an XmlTextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, xGroup); writer.WriteEndElement(); writer.Close(); } } }
public string Namespace {get;}
|
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; namespace Company{ [SoapType("TheGroup", "http://www.cohowinery.com")] public class Group { public string GroupName; public Thing[] Things; [SoapElement(DataType = "language")] public string Lang = "en"; [SoapElement(DataType = "integer")] public string MyNumber; [SoapElement(DataType = "duration")] public string ReDate = "8/31/01"; } public class Thing{ public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.GetMap("MyMap.xml"); } public void GetMap(string filename){ // Create an XmlSerializer instance. XmlTypeMapping map = new SoapReflectionImporter().ImportTypeMapping(typeof(Group)); Console.WriteLine("ElementName: " + map.ElementName); Console.WriteLine("Namespace: " + map.Namespace); Console.WriteLine("TypeFullName: " + map.TypeFullName); Console.WriteLine("TypeName: " + map.TypeName); XmlSerializer ser = new XmlSerializer(map); Group xGroup= new Group(); xGroup.GroupName= "MyCar"; xGroup.MyNumber= 5454.ToString(); xGroup.Things = new Thing[]{new Thing(), new Thing()}; // To write the outer wrapper, use an XmlTextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, xGroup); writer.WriteEndElement(); writer.Close(); } } }
public string TypeFullName {get;}
|
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; namespace Company{ [SoapType("TheGroup", "http://www.cohowinery.com")] public class Group { public string GroupName; public Thing[] Things; [SoapElement(DataType = "language")] public string Lang = "en"; [SoapElement(DataType = "integer")] public string MyNumber; [SoapElement(DataType = "duration")] public string ReDate = "8/31/01"; } public class Thing{ public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.GetMap("MyMap.xml"); } public void GetMap(string filename){ // Create an XmlSerializer instance. XmlTypeMapping map = new SoapReflectionImporter().ImportTypeMapping(typeof(Group)); Console.WriteLine("ElementName: " + map.ElementName); Console.WriteLine("Namespace: " + map.Namespace); Console.WriteLine("TypeFullName: " + map.TypeFullName); Console.WriteLine("TypeName: " + map.TypeName); XmlSerializer ser = new XmlSerializer(map); Group xGroup= new Group(); xGroup.GroupName= "MyCar"; xGroup.MyNumber= 5454.ToString(); xGroup.Things = new Thing[]{new Thing(), new Thing()}; // To write the outer wrapper, use an XmlTextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, xGroup); writer.WriteEndElement(); writer.Close(); } } }
public string TypeName {get;}
|
You can also see the fully qualified name by examining the XmlTypeMapping.TypeFullName property.
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; using System.Text; namespace Company{ [SoapType("TheGroup", "http://www.cohowinery.com")] public class Group { public string GroupName; public Thing[] Things; [SoapElement(DataType = "language")] public string Lang = "en"; [SoapElement(DataType = "integer")] public string MyNumber; [SoapElement(DataType = "duration")] public string ReDate = "8/31/01"; } public class Thing{ public string ThingName; } public class Test { public static void Main() { Test t = new Test(); t.GetMap("MyMap.xml"); } public void GetMap(string filename){ // Create an XmlSerializer instance. XmlTypeMapping map = new SoapReflectionImporter().ImportTypeMapping(typeof(Group)); Console.WriteLine("ElementName: " + map.ElementName); Console.WriteLine("Namespace: " + map.Namespace); Console.WriteLine("TypeFullName: " + map.TypeFullName); Console.WriteLine("TypeName: " + map.TypeName); XmlSerializer ser = new XmlSerializer(map); Group xGroup= new Group(); xGroup.GroupName= "MyCar"; xGroup.MyNumber= 5454.ToString(); xGroup.Things = new Thing[]{new Thing(), new Thing()}; // To write the outer wrapper, use an XmlTextWriter. XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartElement("wrapper"); ser.Serialize(writer, xGroup); writer.WriteEndElement(); writer.Close(); } } }
~XmlTypeMapping(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |