System.Xml.Serialization.XmlTypeMapping Class

Assembly: System.Xml.dll
Namespace: System.Xml.Serialization
Summary
Contains a mapping of one type to another.
C# Syntax:
public class XmlTypeMapping : XmlMapping
Remarks
The XmlTypeMapping class is used to serialize an object as encoded SOAP XML. The resulting XML conforms to section 5 of the World Wide Web Consortium (www.w3.org) document, "Simple Object Access Protocol (SOAP) 1.1". Create an XmlTypeMapping by calling the SoapReflectionImporter.ImportTypeMapping method of the SoapReflectionImporter class. Use the XmlTypeMapping to construct an instance of the XmlSerializer class. To control the serialization, use one of the attributes listed in the conceptual topic at MSDN: attributesthatcontrolsoapencodedserialization.
Example
The following example serializes an instance of a class named 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();
   }
}

    
See also:
System.Xml.Serialization Namespace See also:
MSDN: introducingxmlserialization | MSDN: xmlserializationusingsoapprotocol | MSDN: attributesthatcontrolsoapencodedserialization | MSDN: anexampleofxmlserializationwithxmlserializer

System.Xml.Serialization.XmlTypeMapping Member List:

Public Properties
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.
Public Methods
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.
Protected Methods
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:


System.Xml.Serialization.XmlTypeMapping Member Details

Property: ElementName (read-only)
Summary
Gets the XML element name of the mapped object.
C# Syntax:
public string ElementName {get;}
Remarks
To set an alternate element name of an object, apply a SoapTypeAttribute to the class, and set the SoapTypeAttribute.TypeName property to a new value.
Example
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();
   }
}
}

    

Return to top


Property: Namespace (read-only)
Summary
Gets the XML namespace of the mapped object.
C# Syntax:
public string Namespace {get;}
Remarks
To set a namespace name of an object, apply a SoapTypeAttribute to the class, and set the SoapTypeAttribute.Namespace property to a new value.
Example
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();
   }
}
}

    

Return to top


Property: TypeFullName (read-only)
Summary
The fully qualified type name, including the namespace (or namespaces) and type.
C# Syntax:
public string TypeFullName {get;}
Example
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();
   }
}
}

    

Return to top


Property: TypeName (read-only)
Summary
Gets the type name of the mapped object.
C# Syntax:
public string TypeName {get;}
Remarks
The type name is the class name of the mapped object.

You can also see the fully qualified name by examining the XmlTypeMapping.TypeFullName property.

Example
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();
   }
}
}

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlTypeMapping();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.