SoapTypeAttribute Klasse
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Steuert das vom XmlSerializer generierte Schema, wenn eine Klasseninstanz als SOAP-codiertes XML serialisiert wird.
public ref class SoapTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public class SoapTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type SoapTypeAttribute = class
inherit Attribute
Public Class SoapTypeAttribute
Inherits Attribute
- Vererbung
- Attribute
Beispiele
Im folgenden Beispiel wird eine Klasse mit dem Namen Groupserialisiert. Dies SoapTypeAttribute wird auf die Klasse angewendet, wobei der TypeName Wert auf "SoapGroupType" festgelegt ist. Dies SoapTypeAttribute wird auch außer Kraft gesetzt und in TypeName "Team" geändert. Beide Versionen werden serialisiert, was zu zwei Dateien führt: SoapType.xml und SoapType2.xml.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
// The SoapType is overridden when the
// SerializeOverride method is called.
[SoapType("SoapGroupType", "http://www.cohowinery.com")]
public class Group
{
public string GroupName;
public Employee[] Employees;
}
[SoapType("EmployeeType")]
public class Employee
{
public string Name;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeOriginal("SoapType.xml");
test.SerializeOverride("SoapType2.xml");
test.DeserializeObject("SoapType2.xml");
}
public void SerializeOriginal(string filename)
{
// Create an instance of the XmlSerializer class that
// can be used for serializing as a SOAP message.
XmlTypeMapping mapp =
(new SoapReflectionImporter()).ImportTypeMapping(typeof(Group));
XmlSerializer mySerializer = new XmlSerializer(mapp);
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an XML text writer.
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.GroupName = ".NET";
Employee e1 = new Employee();
e1.Name = "Pat";
myGroup.Employees=new Employee[]{e1};
// Write the root element.
xmlWriter.WriteStartElement("root");
// Serialize the class.
mySerializer.Serialize(xmlWriter, myGroup);
// Close the root tag.
xmlWriter.WriteEndElement();
// Close the XmlWriter.
xmlWriter.Close();
// Close the TextWriter.
writer.Close();
}
public void SerializeOverride(string filename)
{
// Create an instance of the XmlSerializer class that
// uses a SoapAttributeOverrides object.
XmlSerializer mySerializer = CreateOverrideSerializer();
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an XML text writer.
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 2;
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.GroupName = ".NET";
Employee e1 = new Employee();
e1.Name = "Pat";
myGroup.Employees=new Employee[]{e1};
// Write the root element.
xmlWriter.WriteStartElement("root");
// Serialize the class.
mySerializer.Serialize(xmlWriter, myGroup);
// Close the root tag.
xmlWriter.WriteEndElement();
// Close the XmlWriter.
xmlWriter.Close();
// Close the TextWriter.
writer.Close();
}
private XmlSerializer CreateOverrideSerializer()
{
// Create and return an XmlSerializer instance used to
// override and create SOAP messages.
SoapAttributeOverrides mySoapAttributeOverrides =
new SoapAttributeOverrides();
SoapAttributes soapAtts = new SoapAttributes();
// Override the SoapTypeAttribute.
SoapTypeAttribute soapType = new SoapTypeAttribute();
soapType.TypeName = "Team";
soapType.IncludeInSchema = false;
soapType.Namespace = "http://www.microsoft.com";
soapAtts.SoapType = soapType;
mySoapAttributeOverrides.Add(typeof(Group),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(typeof(Group));
XmlSerializer ser = new XmlSerializer(myMapping);
return ser;
}
public void DeserializeObject(string filename)
{
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = CreateOverrideSerializer();
// Reading the file requires a TextReader.
TextReader reader = new StreamReader(filename);
// Create an XML text reader.
XmlTextReader xmlReader = new XmlTextReader(reader);
xmlReader.ReadStartElement();
// Deserialize and cast the object.
Group myGroup = (Group) mySerializer.Deserialize(xmlReader);
xmlReader.ReadEndElement();
Console.WriteLine("The GroupName is " + myGroup.GroupName);
Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " +
"files for the generated XML.");
// Close the readers.
xmlReader.Close();
reader.Close();
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' The SoapType is overridden when the
' SerializeOverride method is called.
<SoapType("SoapGroupType", "http://www.cohowinery.com")> _
Public class Group
Public GroupName As String
Public Employees() As Employee
End Class
<SoapType("EmployeeType")> _
Public Class Employee
Public Name As String
End Class
Public class Run
Public Shared Sub Main()
Dim test As Run = New Run()
test.SerializeOriginal("SoapType.xml")
test.SerializeOverride("SoapType2.xml")
test.DeserializeObject("SoapType2.xml")
End Sub
Public Sub SerializeOriginal(filename As String )
' Create an instance of the XmlSerializer class that
' can be used for serializing as a SOAP message.
Dim mapp As XmlTypeMapping = _
(New SoapReflectionImporter()).ImportTypeMapping(GetType(Group))
Dim mySerializer As XmlSerializer = _
New XmlSerializer(mapp)
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(filename)
' Create an XML text writer.
Dim xmlWriter As XmlTextWriter = New XmlTextWriter(writer)
xmlWriter.Formatting = Formatting.Indented
xmlWriter.Indentation = 2
' Create an instance of the class that will be serialized.
Dim myGroup As Group = New Group()
' Set the object properties.
myGroup.GroupName = ".NET"
Dim e1 As Employee = New Employee()
e1.Name = "Pat"
myGroup.Employees=New Employee(){e1}
' Write the root element.
xmlWriter.WriteStartElement("root")
' Serialize the class.
mySerializer.Serialize(xmlWriter, myGroup)
' Close the root tag.
xmlWriter.WriteEndElement()
' Close the XmlWriter.
xmlWriter.Close()
' Close the TextWriter.
writer.Close()
End Sub
Public Sub SerializeOverride(filename As string )
' Create an instance of the XmlSerializer class that
' uses a SoapAttributeOverrides object.
Dim mySerializer As XmlSerializer = CreateOverrideSerializer()
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(filename)
' Create an XML text writer.
Dim xmlWriter As XmlTextWriter = New XmlTextWriter(writer)
xmlWriter.Formatting = Formatting.Indented
xmlWriter.Indentation = 2
' Create an instance of the class that will be serialized.
Dim myGroup As Group = New Group()
' Set the object properties.
myGroup.GroupName = ".NET"
Dim e1 As Employee = New Employee()
e1.Name = "Pat"
myGroup.Employees = New Employee(){e1}
' Write the root element.
xmlWriter.WriteStartElement("root")
' Serialize the class.
mySerializer.Serialize(xmlWriter, myGroup)
' Close the root tag.
xmlWriter.WriteEndElement()
' Close the XmlWriter.
xmlWriter.Close()
' Close the TextWriter.
writer.Close()
End Sub
Private Function CreateOverrideSerializer() As XmlSerializer
' Create and return an XmlSerializer instance used to
' override and create SOAP messages.
Dim mySoapAttributeOverrides As SoapAttributeOverrides = _
New SoapAttributeOverrides()
Dim soapAtts As SoapAttributes = New SoapAttributes()
' Override the SoapTypeAttribute.
Dim soapType As SoapTypeAttribute = New SoapTypeAttribute()
soapType.TypeName = "Team"
soapType.IncludeInSchema = false
soapType.Namespace = "http://www.microsoft.com"
soapAtts.SoapType = soapType
mySoapAttributeOverrides.Add(GetType(Group),soapAtts)
' Create an XmlTypeMapping that is used to create an instance
' of the XmlSerializer. Then return the XmlSerializer object.
Dim myMapping As XmlTypeMapping = (New SoapReflectionImporter( _
mySoapAttributeOverrides)).ImportTypeMapping(GetType(Group))
Dim ser As XmlSerializer = New XmlSerializer(myMapping)
return ser
End Function
Public Sub DeserializeObject(filename As String)
' Create an instance of the XmlSerializer class.
Dim mySerializer As XmlSerializer = CreateOverrideSerializer()
' Reading the file requires a TextReader.
Dim reader As TextReader = New StreamReader(filename)
' Create an XML text reader.
Dim xmlReader As XmlTextReader = New XmlTextReader(reader)
xmlReader.ReadStartElement()
' Deserialize and cast the object.
Dim myGroup As Group = CType(mySerializer.Deserialize(xmlReader), Group)
xmlReader.ReadEndElement()
Console.WriteLine("The GroupName is " + myGroup.GroupName)
Console.WriteLine("Look at the SoapType.xml and SoapType2.xml " + _
"files for the generated XML.")
' Close the readers.
xmlReader.Close()
reader.Close()
End Sub
End Class
Hinweise
Die SoapTypeAttribute Klasse gehört zu einer Reihe von Attributen, die steuern, wie das XmlSerializer Serialisieren oder Deserialisieren eines Objekts als codierter SOAP-XML-Code gesteuert wird. Die resultierende XML entspricht Abschnitt 5 des World Wide Web Consortium-Dokuments, Simple Object Access Protocol (SOAP) 1.1. Eine vollständige Liste mit ähnlichen Attributen finden Sie unter Attribute That Control Encoded SOAP Serialization.
Um ein Objekt als codierte SOAP-Nachricht zu serialisieren, erstellen Sie die XmlSerializer Verwendung einer XmlTypeMapping erstellten Mit der ImportTypeMapping Methode der SoapReflectionImporter Klasse.
Dies SoapTypeAttribute kann nur auf Klassendeklarationen angewendet werden.
Die IncludeInSchema Eigenschaft bestimmt, ob der resultierende XML-Elementtyp im XML-Schemadokument (XSD) für den generierten XML-Datenstrom enthalten ist. Um das Schema anzuzeigen, kompilieren Sie die Klasse in eine DLL-Datei. Übergeben Sie die resultierende Datei als Argument an das XML-Schemadefinitionstool (Xsd.exe). Das Tool generiert das XML-Schema für den XML-Datenstrom, der generiert wird, wenn die Klasse von einer Instanz der XmlSerializer Klasse serialisiert wird.
Durch festlegen eines anderen Namespaces wird Xsd.exe eine andere Schemadatei (XSD) für den XML-Datenstrom schreiben, der generiert wird, wenn die Klasse serialisiert wird.
Konstruktoren
| Name | Beschreibung |
|---|---|
| SoapTypeAttribute() |
Initialisiert eine neue Instanz der SoapTypeAttribute-Klasse. |
| SoapTypeAttribute(String, String) |
Initialisiert eine neue Instanz der SoapTypeAttribute Klasse und gibt den Namen und den XML-Namespace des Typs an. |
| SoapTypeAttribute(String) |
Initialisiert eine neue Instanz der SoapTypeAttribute Klasse und gibt den Namen des XML-Typs an. |
Eigenschaften
| Name | Beschreibung |
|---|---|
| IncludeInSchema |
Dient zum Abrufen oder Festlegen eines Werts, der angibt, ob der Typ in SOAP-codierte XML-Schemadokumente eingeschlossen werden soll. |
| Namespace |
Ruft den Namespace des XML-Typs ab oder legt den Namespace fest. |
| TypeId |
Wenn sie in einer abgeleiteten Klasse implementiert wird, wird ein eindeutiger Bezeichner für diese Attribute. (Geerbt von Attribute) |
| TypeName |
Dient zum Abrufen oder Festlegen des Namens des XML-Typs. |
Methoden
| Name | Beschreibung |
|---|---|
| Equals(Object) |
Gibt einen Wert zurück, der angibt, ob diese Instanz einem angegebenen Objekt entspricht. (Geerbt von Attribute) |
| GetHashCode() |
Gibt den Hashcode für diese Instanz zurück. (Geerbt von Attribute) |
| GetType() |
Ruft die Type der aktuellen Instanz ab. (Geerbt von Object) |
| IsDefaultAttribute() |
Wenn sie in einer abgeleiteten Klasse überschrieben wird, gibt an, ob der Wert dieser Instanz der Standardwert für die abgeleitete Klasse ist. (Geerbt von Attribute) |
| Match(Object) |
Wenn sie in einer abgeleiteten Klasse überschrieben wird, wird ein Wert zurückgegeben, der angibt, ob diese Instanz einem angegebenen Objekt entspricht. (Geerbt von Attribute) |
| MemberwiseClone() |
Erstellt eine flache Kopie der aktuellen Object. (Geerbt von Object) |
| ToString() |
Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt. (Geerbt von Object) |
Explizite Schnittstellenimplementierungen
| Name | Beschreibung |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Ordnet eine Reihe von Namen einer entsprechenden Reihe von Dispatchbezeichnern zu. (Geerbt von Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Ruft die Typinformationen für ein Objekt ab, mit denen die Typinformationen für eine Schnittstelle abgerufen werden können. (Geerbt von Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Ruft die Anzahl der Schnittstellen mit Typinformationen ab, die von einem Objekt bereitgestellt werden (0 oder 1). (Geerbt von Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Bietet Zugriff auf Eigenschaften und Methoden, die von einem Objekt verfügbar gemacht werden. (Geerbt von Attribute) |