Export (0) Print
Expand All

XmlTypeAttribute Constructor (String)

Initializes a new instance of the XmlTypeAttribute class and specifies the name of the XML type.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

public XmlTypeAttribute(
	string typeName


Type: System.String

The name of the XML type that the XmlSerializer generates when it serializes the class instance (and recognizes when it deserializes the class instance).

Apply the XmlTypeAttribute to a class to specify the XML type's namespace, the XML type name, and whether to include the type in the XML schema document. To see the results of setting the properties of the XmlTypeAttribute class, compile your application as an executable or DLL, and pass the resulting file to the XML Schema Definition Tool (Xsd.exe). The tool writes the schema, including the type definition.

The following example creates two instances of the XmlTypeAttribute class that are used to override the serialization of the two classes.

using System;
using System.IO;
using System.Xml.Serialization;

public class Person
   public string personName;
   public Address address;
public class Address
   public string state;
   public string zip;

public class PersonTypeAttribute
   public static void Main()
      PersonTypeAttribute myPersonTypeAttribute= new PersonTypeAttribute();

   public XmlSerializer CreateOverrider()
      XmlAttributeOverrides personOverride = new XmlAttributeOverrides();      

      XmlAttributes personAttributes = new XmlAttributes();      
      XmlTypeAttribute personType = new XmlTypeAttribute();
      personType.TypeName = "Employee";
      personType.Namespace = "http://www.microsoft.com";
      personAttributes.XmlType = personType;

      XmlAttributes addressAttributes = new XmlAttributes();
      // Create 'XmlTypeAttribute' with 'TypeName' as an argument.
      XmlTypeAttribute addressType = new XmlTypeAttribute("Address");
      addressType.Namespace = "http://www.microsoft.com";

      personOverride.Add(typeof(Person) ,personAttributes);

      XmlSerializer myXmlSerializer = new XmlSerializer
         (typeof(Person), personOverride);
      return myXmlSerializer;

   public void SerializeObject(string filename)
      XmlSerializer myXmlSerializer = CreateOverrider();

      Address myAddress = new Address();

      Person myPerson = new Person();
      // Serialize to a file.
      TextWriter writer = new StreamWriter(filename);
      myXmlSerializer.Serialize(writer, myPerson);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft