Freigeben über


XmlTypeAttribute Konstruktoren

Definition

Initialisiert eine neue Instanz der XmlTypeAttribute-Klasse.

Überlädt

XmlTypeAttribute()

Initialisiert eine neue Instanz der XmlTypeAttribute-Klasse.

XmlTypeAttribute(String)

Initialisiert eine neue Instanz der XmlTypeAttribute-Klasse und gibt den Namen des XML-Typs an.

XmlTypeAttribute()

Quelle:
XmlTypeAttribute.cs
Quelle:
XmlTypeAttribute.cs
Quelle:
XmlTypeAttribute.cs

Initialisiert eine neue Instanz der XmlTypeAttribute-Klasse.

public:
 XmlTypeAttribute();
public XmlTypeAttribute ();
Public Sub New ()

Beispiele

Im folgenden Beispiel werden zwei Instanzen der -Klasse erstellt, die XmlTypeAttribute verwendet werden, um die Serialisierung der beiden Klassen zu überschreiben.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Address
{
public:
   String^ state;
   String^ zip;
};

public ref class Person
{
public:
   String^ personName;
   Address^ address;
};

public ref class PersonTypeAttribute
{
public:
   XmlSerializer^ CreateOverrider()
   {
      XmlAttributeOverrides^ personOverride = gcnew XmlAttributeOverrides;
      XmlAttributes^ personAttributes = gcnew XmlAttributes;
      XmlTypeAttribute^ personType = gcnew XmlTypeAttribute;
      personType->TypeName = "Employee";
      personType->Namespace = "http://www.microsoft.com";
      personAttributes->XmlType = personType;
      XmlAttributes^ addressAttributes = gcnew XmlAttributes;

      // Create 'XmlTypeAttribute' with 'TypeName' as an argument.
      XmlTypeAttribute^ addressType = gcnew XmlTypeAttribute( "Address" );
      addressType->Namespace = "http://www.microsoft.com";
      addressAttributes->XmlType = addressType;
      personOverride->Add( Person::typeid, personAttributes );
      personOverride->Add( Address::typeid, addressAttributes );
      XmlSerializer^ myXmlSerializer = gcnew XmlSerializer( Person::typeid,personOverride );
      return myXmlSerializer;
   }

   void SerializeObject( String^ filename )
   {
      XmlSerializer^ myXmlSerializer = CreateOverrider();
      Address^ myAddress = gcnew Address;
      myAddress->state = "AAA";
      myAddress->zip = "11111";
      Person^ myPerson = gcnew Person;
      myPerson->personName = "Smith";
      myPerson->address = myAddress;

      // Serialize to a file.
      TextWriter^ writer = gcnew StreamWriter( filename );
      myXmlSerializer->Serialize( writer, myPerson );
   }
};

int main()
{
   PersonTypeAttribute^ myPersonTypeAttribute = gcnew PersonTypeAttribute;
   myPersonTypeAttribute->SerializeObject( "XmlType.xml" );
}
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();
      myPersonTypeAttribute.SerializeObject("XmlType.xml");
   }
   
   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";
      addressAttributes.XmlType=addressType;

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

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

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

      Address myAddress = new Address();
      myAddress.state="AAA";
      myAddress.zip="11111";

      Person myPerson = new Person();
      myPerson.personName="Smith";
      myPerson.address=myAddress;
      // Serialize to a file.
      TextWriter writer = new StreamWriter(filename);
      myXmlSerializer.Serialize(writer, myPerson);
   }
}
Imports System.IO
Imports System.Xml.Serialization

Public Class Person
   Public personName As String
   Public address As Address
End Class

Public Class Address
   Public state As String
   Public zip As String
End Class

Public Class PersonTypeAttribute

   Public Shared Sub Main()
      Dim myPersonTypeAttribute As New PersonTypeAttribute()
      myPersonTypeAttribute.SerializeObject("XmlType.xml")
   End Sub

   Public Function CreateOverrider() As XmlSerializer
      Dim personOverride As New XmlAttributeOverrides()
      Dim personAttributes As New XmlAttributes()
      Dim personType As New XmlTypeAttribute()
      personType.TypeName = "Employee"
      personType.Namespace = "http://www.microsoft.com"
      personAttributes.XmlType = personType

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

      personOverride.Add(GetType(Person), personAttributes)
      personOverride.Add(GetType(Address), addressAttributes)

      Dim myXmlSerializer As New XmlSerializer(GetType(Person), personOverride)
      Return myXmlSerializer
   End Function

   Public Sub SerializeObject(filename As String)
      Dim myXmlSerializer As XmlSerializer = CreateOverrider()

      Dim myAddress As New Address()
      myAddress.state = "AAA"
      myAddress.zip = "11111"

      Dim myPerson As New Person()
      myPerson.personName = "Smith"
      myPerson.address = myAddress
      ' Serialize to a file.
      Dim writer = New StreamWriter(filename)
      myXmlSerializer.Serialize(writer, myPerson)
   End Sub
End Class

Gilt für:

XmlTypeAttribute(String)

Quelle:
XmlTypeAttribute.cs
Quelle:
XmlTypeAttribute.cs
Quelle:
XmlTypeAttribute.cs

Initialisiert eine neue Instanz der XmlTypeAttribute-Klasse und gibt den Namen des XML-Typs an.

public:
 XmlTypeAttribute(System::String ^ typeName);
public XmlTypeAttribute (string typeName);
public XmlTypeAttribute (string? typeName);
new System.Xml.Serialization.XmlTypeAttribute : string -> System.Xml.Serialization.XmlTypeAttribute
Public Sub New (typeName As String)

Parameter

typeName
String

Der Name des XML-Typs, der vom XmlSerializer beim Serialisieren einer Klasseninstanz generiert bzw. beim Deserialisieren der Klasseninstanz erkannt wird.

Beispiele

Im folgenden Beispiel werden zwei Instanzen der -Klasse erstellt, die XmlTypeAttribute verwendet werden, um die Serialisierung der beiden Klassen zu überschreiben.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Address
{
public:
   String^ state;
   String^ zip;
};

public ref class Person
{
public:
   String^ personName;
   Address^ address;
};

public ref class PersonTypeAttribute
{
public:
   XmlSerializer^ CreateOverrider()
   {
      XmlAttributeOverrides^ personOverride = gcnew XmlAttributeOverrides;
      XmlAttributes^ personAttributes = gcnew XmlAttributes;
      XmlTypeAttribute^ personType = gcnew XmlTypeAttribute;
      personType->TypeName = "Employee";
      personType->Namespace = "http://www.microsoft.com";
      personAttributes->XmlType = personType;
      XmlAttributes^ addressAttributes = gcnew XmlAttributes;

      // Create 'XmlTypeAttribute' with 'TypeName' as an argument.
      XmlTypeAttribute^ addressType = gcnew XmlTypeAttribute( "Address" );
      addressType->Namespace = "http://www.microsoft.com";
      addressAttributes->XmlType = addressType;
      personOverride->Add( Person::typeid, personAttributes );
      personOverride->Add( Address::typeid, addressAttributes );
      XmlSerializer^ myXmlSerializer = gcnew XmlSerializer( Person::typeid,personOverride );
      return myXmlSerializer;
   }

   void SerializeObject( String^ filename )
   {
      XmlSerializer^ myXmlSerializer = CreateOverrider();
      Address^ myAddress = gcnew Address;
      myAddress->state = "AAA";
      myAddress->zip = "11111";
      Person^ myPerson = gcnew Person;
      myPerson->personName = "Smith";
      myPerson->address = myAddress;

      // Serialize to a file.
      TextWriter^ writer = gcnew StreamWriter( filename );
      myXmlSerializer->Serialize( writer, myPerson );
   }
};

int main()
{
   PersonTypeAttribute^ myPersonTypeAttribute = gcnew PersonTypeAttribute;
   myPersonTypeAttribute->SerializeObject( "XmlType.xml" );
}
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();
      myPersonTypeAttribute.SerializeObject("XmlType.xml");
   }
   
   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";
      addressAttributes.XmlType=addressType;

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

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

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

      Address myAddress = new Address();
      myAddress.state="AAA";
      myAddress.zip="11111";

      Person myPerson = new Person();
      myPerson.personName="Smith";
      myPerson.address=myAddress;
      // Serialize to a file.
      TextWriter writer = new StreamWriter(filename);
      myXmlSerializer.Serialize(writer, myPerson);
   }
}
Imports System.IO
Imports System.Xml.Serialization

Public Class Person
   Public personName As String
   Public address As Address
End Class

Public Class Address
   Public state As String
   Public zip As String
End Class

Public Class PersonTypeAttribute

   Public Shared Sub Main()
      Dim myPersonTypeAttribute As New PersonTypeAttribute()
      myPersonTypeAttribute.SerializeObject("XmlType.xml")
   End Sub

   Public Function CreateOverrider() As XmlSerializer
      Dim personOverride As New XmlAttributeOverrides()
      Dim personAttributes As New XmlAttributes()
      Dim personType As New XmlTypeAttribute()
      personType.TypeName = "Employee"
      personType.Namespace = "http://www.microsoft.com"
      personAttributes.XmlType = personType

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

      personOverride.Add(GetType(Person), personAttributes)
      personOverride.Add(GetType(Address), addressAttributes)

      Dim myXmlSerializer As New XmlSerializer(GetType(Person), personOverride)
      Return myXmlSerializer
   End Function

   Public Sub SerializeObject(filename As String)
      Dim myXmlSerializer As XmlSerializer = CreateOverrider()

      Dim myAddress As New Address()
      myAddress.state = "AAA"
      myAddress.zip = "11111"

      Dim myPerson As New Person()
      myPerson.personName = "Smith"
      myPerson.address = myAddress
      ' Serialize to a file.
      Dim writer = New StreamWriter(filename)
      myXmlSerializer.Serialize(writer, myPerson)
   End Sub
End Class

Hinweise

Wenden Sie auf XmlTypeAttribute eine Klasse an, um den Namespace des XML-Typs, den XML-Typnamen und die Angabe, ob der Typ in das XML-Schemadokument eingeschlossen werden soll. Um die Ergebnisse des Festlegens der Eigenschaften der XmlTypeAttribute -Klasse anzuzeigen, kompilieren Sie Ihre Anwendung als ausführbare Datei oder DLL, und übergeben Sie die resultierende Datei an das XML-Schemadefinitionstool (Xsd.exe). Das Tool schreibt das Schema, einschließlich der Typdefinition.

Gilt für: