XmlTypeMapping Clase
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Contiene una asignación de un tipo a otro.
public ref class XmlTypeMapping : System::Xml::Serialization::XmlMapping
public class XmlTypeMapping : System.Xml.Serialization.XmlMapping
type XmlTypeMapping = class
inherit XmlMapping
Public Class XmlTypeMapping
Inherits XmlMapping
- Herencia
Ejemplos
En el ejemplo siguiente se serializa una instancia de una clase denominada Transportation que contiene un campo denominado Vehicle.
SoapElementAttribute Se aplica al campo . Cuando el campo se serializa, el nombre del elemento XML es "Wheels" en lugar de "Vehicle". El SerializeOverride método crea un SoapElementAttribute objeto y establece la SoapElement propiedad de en SoapAttributes .SoapElementAttribute
SoapAttributes se agrega a un SoapAttributeOverrides objeto que se usa para crear un XmlTypeMappingobjeto . Se XmlSerializer construye con XmlTypeMapping, y se vuelve a serializar una instancia de la Transportation clase .
SoapElementAttribute Dado que se usa para invalidar la serialización, el nombre del elemento XML generado ahora es "Truck" en lugar de "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();
}
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Imports System.Text
Public Class Transportation
' The SoapElementAttribute specifies that the
' generated XML element name will be "Wheels"
' instead of "Vehicle".
<SoapElement("Wheels")> Public Vehicle As String
<SoapElement(DataType:= "dateTime")> _
public CreationDate As DateTime
<SoapElement(IsNullable:= true)> _
public thing As Thing
End Class
Public Class Thing
<SoapElement(IsNullable:=true)> public ThingName As string
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.SerializeObject("SoapElementOriginalVb.xml")
t.SerializeOverride("SoapElementOverrideVb.xml")
Console.WriteLine("Finished writing two XML files.")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateSoapOverrider() As XmlSerializer
' Create the SoapAttributes and SoapAttributeOverrides objects.
Dim soapAttrs As SoapAttributes = New SoapAttributes()
Dim soapOverrides As SoapAttributeOverrides = _
New SoapAttributeOverrides()
' Create a SoapElementAttribute to override
' the Vehicles property.
Dim soapElement1 As SoapElementAttribute = _
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(GetType(Transportation), "Vehicle", soapAttrs)
' Create the XmlSerializer, and return it.
Dim myTypeMapping As XmlTypeMapping = (New _
SoapReflectionImporter (soapOverrides)).ImportTypeMapping _
(GetType(Transportation))
return New XmlSerializer(myTypeMapping)
End Function
Public Sub SerializeOverride(filename As String)
' Create an XmlSerializer instance.
Dim ser As XmlSerializer = CreateSoapOverrider()
' Create the object and serialize it.
Dim myTransportation As Transportation = _
New Transportation()
myTransportation.Vehicle = "MyCar"
myTransportation.CreationDate = DateTime.Now
myTransportation.thing= new Thing()
Dim writer As XmlTextWriter = _
New XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
ser.Serialize(writer, myTransportation)
writer.WriteEndElement()
writer.Close()
End Sub
Public Sub SerializeObject(filename As String)
' Create an XmlSerializer instance.
Dim ser As XmlSerializer = _
New XmlSerializer(GetType(Transportation))
Dim myTransportation As Transportation = _
New Transportation()
myTransportation.Vehicle = "MyCar"
myTransportation.CreationDate=DateTime.Now
myTransportation.thing= new Thing()
Dim writer As XmlTextWriter = _
new XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
ser.Serialize(writer, myTransportation)
writer.WriteEndElement()
writer.Close()
End Sub
End Class
Comentarios
La XmlTypeMapping clase se usa para serializar un objeto como XML SOAP codificado. El XML resultante se ajusta a la sección 5 del documento World Wide Web Consortium , "Protocolo simple de acceso a objetos (SOAP) 1.1". Cree un XmlTypeMapping mediante una llamada al ImportTypeMapping método de la SoapReflectionImporter clase . XmlTypeMapping Use para construir una instancia de la XmlSerializer clase . Para controlar la serialización, use uno de los atributos enumerados en Atributos que controlan la serialización SOAP codificada.
Propiedades
| Nombre | Description |
|---|---|
| ElementName |
Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código. Obtiene el nombre del elemento asignado. |
| ElementName |
Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código. Obtiene el nombre del elemento asignado. (Heredado de XmlMapping) |
| Namespace |
Obtiene el espacio de nombres XML del objeto asignado. |
| Namespace |
Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código. Obtiene el espacio de nombres del elemento asignado. (Heredado de XmlMapping) |
| TypeFullName |
Nombre de tipo completo que incluye el espacio de nombres (o espacios de nombres) y el tipo. |
| TypeName |
Obtiene el nombre de tipo del objeto asignado. |
| XsdElementName |
Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código. Obtiene el nombre del elemento XSD de la asignación. (Heredado de XmlMapping) |
| XsdTypeName |
Obtiene el nombre del elemento XML del objeto asignado. |
| XsdTypeNamespace |
Obtiene el espacio de nombres XML del objeto asignado. |
Métodos
| Nombre | Description |
|---|---|
| Equals(Object) |
Determina si el objeto especificado es igual que el objeto actual. (Heredado de Object) |
| GetHashCode() |
Sirve como la función hash predeterminada. (Heredado de Object) |
| GetType() |
Obtiene el Type de la instancia actual. (Heredado de Object) |
| MemberwiseClone() |
Crea una copia superficial del Object actual. (Heredado de Object) |
| SetKey(String) |
Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código. Establece la clave utilizada para buscar la asignación. (Heredado de XmlMapping) |
| ToString() |
Devuelve una cadena que representa el objeto actual. (Heredado de Object) |