XmlTypeMapping 類別

定義

包含某個類型與其他類型的對應。

C#
public class XmlTypeMapping : System.Xml.Serialization.XmlMapping
繼承
XmlTypeMapping

範例

下列範例會序列化名為 Transportation 的類別實例,其中包含名為 Vehicle 的欄位。 會 SoapElementAttribute 套用至 欄位。 序列化欄位時,XML 專案名稱為 「Wheel」,而不是 「Vehicle」。 方法 SerializeOverrideSoapElementAttribute 建立 ,並將 的 SoapAttributes 屬性設定 SoapElementSoapElementAttribute 。 會 SoapAttributes 加入至 SoapAttributeOverrides ,用來建立 XmlTypeMappingXmlSerializer會使用 XmlTypeMapping 建構 ,而 類別的 Transportation 實例會再次序列化。 SoapElementAttribute因為 是用來覆寫序列化,所以產生的 XML 專案名稱現在是 「Truck」,而不是 「Wheel」。

C#
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();
   }
}

備註

類別 XmlTypeMapping 是用來將物件序列化為編碼的 SOAP XML。 產生的 XML 符合 全球資訊網協會 檔「Simple Object Access Protocol (SOAP) 1.1」 的第 5 節。 XmlTypeMapping呼叫 ImportTypeMapping 類別的 方法, SoapReflectionImporter 以建立 。 XmlTypeMapping使用 來建構 類別的 XmlSerializer 實例。 若要控制序列化,請使用 控制編碼 SOAP 序列化之屬性中列出的其中一個屬性。

屬性

ElementName

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

取得對應之元素的名稱。

ElementName

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

取得對應之元素的名稱。

(繼承來源 XmlMapping)
Namespace

取得對應物件的 XML 命名空間。

Namespace

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

取得對應之項目的命名空間。

(繼承來源 XmlMapping)
TypeFullName

完整類型名稱,其中包含命名空間和類型。

TypeName

取得對應物件的類型名稱。

XsdElementName

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

取得對應之 XSD 項目的名稱。

(繼承來源 XmlMapping)
XsdTypeName

取得對應物件的 XML 項目名稱。

XsdTypeNamespace

取得對應物件的 XML 命名空間。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetKey(String)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

設定用來查閱對應的索引鍵。

(繼承來源 XmlMapping)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱