SoapElementAttribute.DataType 属性

定义

获取或设置生成的 XML 元素的“XML 架构”定义语言 (XSD) 数据类型。Gets or sets the XML Schema definition language (XSD) data type of the generated XML element.

public:
 property System::String ^ DataType { System::String ^ get(); void set(System::String ^ value); };
public string DataType { get; set; }
member this.DataType : string with get, set
Public Property DataType As String

属性值

String

“XML 架构”数据类型之一。One of the XML Schema data types.

示例

下面的示例对名为的类的实例 Transportation 进行序列化,该类包含一个名为的字段 VehicleThe following example serializes an instance of a class named Transportation that contains a field named Vehicle. SoapElementAttribute应用于字段。A SoapElementAttribute is applied to the field. 如果对字段进行序列化,则 XML 元素名称为 "车轮" 而不是 "车辆"。When the field is serialized, the XML element name is "Wheels" instead of "Vehicle". SerializeOverride方法创建一个 SoapElementAttribute ,并将的 SoapElement 属性设置 SoapAttributesSoapElementAttributeThe SerializeOverride method creates a SoapElementAttribute and sets the SoapElement property of a SoapAttributes to the SoapElementAttribute. SoapAttributes 添加到 SoapAttributeOverrides 用于创建的 XmlTypeMappingThe SoapAttributes is added to a SoapAttributeOverrides that is used to create an XmlTypeMapping. XmlSerializer使用构造 XmlTypeMapping ,并 Transportation 再次序列化类的实例。An XmlSerializer is constructed with the XmlTypeMapping, and an instance of the Transportation class is again serialized. 由于 SoapElementAttribute 用于重写序列化,因此生成的 XML 元素名称现在为 "卡车" 而不是 "车轮"。Because the SoapElementAttribute is used to override the serialization, the generated XML element name is now "Truck" instead of "Wheels".

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Collections;
using namespace System::Xml;
using namespace System::Text;
public ref class Thing
{
public:

   [SoapElement(IsNullable=true)]
   String^ ThingName;
};

public ref class Transportation
{
public:

   // The SoapElementAttribute specifies that the
   // generated XML element name will be S"Wheels"
   // instead of S"Vehicle".

   [SoapElement("Wheels")]
   String^ Vehicle;

   [SoapElement(DataType="dateTime")]
   DateTime CreationDate;

   [SoapElement(IsNullable=true)]
   Thing^ thing;
};

public ref class Test
{
public:

   // Return an XmlSerializer used for overriding.
   XmlSerializer^ CreateSoapOverrider()
   {
      // Create the SoapAttributes and SoapAttributeOverrides objects.
      SoapAttributes^ soapAttrs = gcnew SoapAttributes;
      SoapAttributeOverrides^ soapOverrides = gcnew SoapAttributeOverrides;

      // Create an SoapElementAttribute to the Vehicles property.
      SoapElementAttribute^ soapElement1 = gcnew SoapElementAttribute( "Truck" );

      // Set the SoapElement to the Object*.
      soapAttrs->SoapElement = soapElement1;

      // Add the SoapAttributes to the SoapAttributeOverrides,specifying the member to.
      soapOverrides->Add( Transportation::typeid, "Vehicle", soapAttrs );

      // Create the XmlSerializer, and return it.
      XmlTypeMapping^ myTypeMapping = (gcnew SoapReflectionImporter( soapOverrides ))->ImportTypeMapping( Transportation::typeid );
      return gcnew XmlSerializer( myTypeMapping );
   }

   void SerializeOverride( String^ filename )
   {
      // Create an XmlSerializer instance.
      XmlSerializer^ ser = CreateSoapOverrider();

      // Create the Object* and serialize it.
      Transportation^ myTransportation = gcnew Transportation;
      myTransportation->Vehicle = "MyCar";
      myTransportation->CreationDate = DateTime::Now;
      myTransportation->thing = gcnew Thing;
      XmlTextWriter^ writer = gcnew XmlTextWriter( filename,Encoding::UTF8 );
      writer->Formatting = Formatting::Indented;
      writer->WriteStartElement( "wrapper" );
      ser->Serialize( writer, myTransportation );
      writer->WriteEndElement();
      writer->Close();
   }

   void SerializeObject( String^ filename )
   {
      // Create an XmlSerializer instance.
      XmlSerializer^ ser = gcnew XmlSerializer( Transportation::typeid );
      Transportation^ myTransportation = gcnew Transportation;
      myTransportation->Vehicle = "MyCar";
      myTransportation->CreationDate = DateTime::Now;
      myTransportation->thing = gcnew Thing;
      XmlTextWriter^ writer = gcnew XmlTextWriter( filename,Encoding::UTF8 );
      writer->Formatting = Formatting::Indented;
      writer->WriteStartElement( "wrapper" );
      ser->Serialize( writer, myTransportation );
      writer->WriteEndElement();
      writer->Close();
   }
};

int main()
{
   Test^ t = gcnew Test;
   t->SerializeObject( "SoapElementOriginal.xml" );
   t->SerializeOverride( "SoapElementOverride.xml" );
   Console::WriteLine( "Finished writing two XML files." );
}
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

注解

下表列出了具有 .NET 等效项的 XML 架构简单数据类型。The following table lists the XML Schema simple data types with their .NET equivalents.

对于 XML 架构 base64BinaryhexBinary 数据类型,请使用结构的数组 Byte ,并 SoapElementAttribute DataType 根据需要将设置为 "base64Binary" 或 "hexBinary" 的。For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a SoapElementAttribute with the DataType set to "base64Binary" or "hexBinary", as appropriate. 对于 XML 架构 timedate 数据类型,请使用 DateTime 类型,并将 SoapElementAttribute DataType 设置为 "日期" 或 "时间"。For the XML Schema time and date data types, use the DateTime type and apply the SoapElementAttribute with the DataType set to "date" or "time".

对于映射到字符串的每个 XML 架构数据类型,应用 SoapElementAttributeDataType 属性设置为 XML 架构类型的。For every XML Schema data type that is mapped to a string, apply the SoapElementAttribute with its DataType property set to the XML Schema type. 请注意,这不会更改序列化格式,而只会更改成员的架构。Note that this does not change the serialization format, only the schema for the member.

备注

属性区分大小写,因此必须将其精确设置为 XML 架构数据类型之一。The property is case-sensitive, so you must set it exactly to one of the XML Schema data types.

备注

将二进制数据作为 XML 元素传递比将其作为 XML 特性传递更为有效。Passing binary data as an XML element is more efficient than passing it as an XML attribute.

有关 XML 数据类型的详细信息,请参阅万维网联合会文档 Xml 架构第2部分:数据类型。For more information about XML data types, see the World Wide Web Consortium document, XML Schema Part 2: Datatypes.

XSD 数据类型XSD data type .NET 数据类型.NET data type
anyURIanyURI String
base64Binarybase64Binary 对象数组 ByteArray of Byte objects
booleanboolean Boolean
字节byte SByte
日期date DateTime
dateTimedateTime DateTime
Decimaldecimal Decimal
Doubledouble Double
ENTITYENTITY String
条目ENTITIES String
FLOATfloat Single
gDaygDay String
gMonthgMonth String
gMonthDaygMonthDay String
gYeargYear String
gYearMonthgYearMonth String
hexBinaryhexBinary 对象数组 ByteArray of Byte objects
IDID String
IDREFIDREF String
IDREFSIDREFS String
intint Int32
整型integer String
语言language String
longlong Int64
“属性”Name String
NCNameNCName String
negativeIntegernegativeInteger String
NMTOKENNMTOKEN String
NMTOKENSNMTOKENS String
normalizedStringnormalizedString String
nonNegativeIntegernonNegativeInteger String
nonPositiveIntegernonPositiveInteger String
NOTATIONNOTATION String
positiveIntegerpositiveInteger String
QNameQName XmlQualifiedName
durationduration String
字符串string String
shortshort Int16
timetime DateTime
令牌token String
unsignedByteunsignedByte Byte
unsignedIntunsignedInt UInt32
unsignedLongunsignedLong UInt64
unsignedShortunsignedShort UInt16

适用于