HOW TO:指定 XML 資料流的替代項目名稱How to: Specify an Alternate Element Name for an XML Stream

使用 XmlSerializer,以相同類別集,可產生一個以上的 XML 資料流。Using the XmlSerializer, you can generate more than one XML stream with the same set of classes. 當兩個不同的 XML Web 服務要求相同的基本資訊以及些微差異時,您也許會想這麼做。You might want to do this because two different XML Web services require the same basic information, with only slight differences. 例如,試想有兩家處理書本訂單的 XML Web 服務,而且都需要 ISBN 號碼。For example, imagine two XML Web services that process orders for books, and thus both require ISBN numbers. 一個服務使用標記 <ISBN>,另一個則使用標記 <BookID>。One service uses the tag <ISBN> while the second uses the tag <BookID>. 您有名為 Book 的類別,其中包含名為 ISBN的欄位。You have a class named Book that contains a field named ISBN. Book 類別的執行個體序列化時,它會根據預設使用成員名稱 (ISBN) 做為標記項目名稱。When an instance of the Book class is serialized, it will, by default, use the member name (ISBN) as the tag element name. 對於第一個 XML Web 服務,這正如預期。For the first XML Web service, this is as expected. 不過要想要傳送 XML 資料流至第二個 XML Web 服務,您必須覆寫序列化,讓標記的項目名稱為 BookIDBut to send the XML stream to the second XML Web service, you must override the serialization so that the tag's element name is BookID.

以其他項目名稱建立 XML 資料流To create an XML stream with an alternate element name

  1. 建立 XmlElementAttribute 類別的執行個體。Create an instance of the XmlElementAttribute class.

  2. ElementNameXmlElementAttribute 設為 "BookID"。Set the ElementName of the XmlElementAttribute to "BookID".

  3. 建立 XmlAttributes 類別的執行個體。Create an instance of the XmlAttributes class.

  4. XmlElementAttribute 物件加入至透過 XmlElementsXmlAttributes 屬性存取的集合。Add the XmlElementAttribute object to the collection accessed through the XmlElements property of XmlAttributes .

  5. 建立 XmlAttributeOverrides 類別的執行個體。Create an instance of the XmlAttributeOverrides class.

  6. XmlAttributes 加入至 XmlAttributeOverrides,傳遞要覆寫的物件型別及遭覆寫的成員名稱。Add the XmlAttributes to the XmlAttributeOverrides, passing the type of the object to override and the name of the member being overridden.

  7. 使用 XmlSerializer``XmlAttributeOverrides建立 類別的執行個體。Create an instance of the XmlSerializer class with XmlAttributeOverrides.

  8. 建立 Book 類別的執行個體,將它序列化或還原序列化。Create an instance of the Book class, and serialize or deserialize it.

範例Example

Public Class SerializeOverride()  
    ' Creates an XmlElementAttribute with the alternate name.  
    Dim myElementAttribute As XmlElementAttribute = _  
    New XmlElementAttribute()  
    myElementAttribute.ElementName = "BookID"  
    Dim myAttributes As XmlAttributes = New XmlAttributes()  
    myAttributes.XmlElements.Add(myElementAttribute)  
    Dim myOverrides As XmlAttributeOverrides = New XmlAttributeOverrides()  
    myOverrides.Add(typeof(Book), "ISBN", myAttributes)  
    Dim mySerializer As XmlSerializer = _  
    New XmlSerializer(GetType(Book), myOverrides)  
    Dim b As Book = New Book()  
    b.ISBN = "123456789"  
    ' Creates a StreamWriter to write the XML stream to.  
    Dim writer As StreamWriter = New StreamWriter("Book.xml")  
    mySerializer.Serialize(writer, b);  
End Class  
public class SerializeOverride()  
{  
    // Creates an XmlElementAttribute with the alternate name.  
    XmlElementAttribute myElementAttribute = new XmlElementAttribute();  
    myElementAttribute.ElementName = "BookID";  
    XmlAttributes myAttributes = new XmlAttributes();  
    myAttributes.XmlElements.Add(myElementAttribute);  
    XmlAttributeOverrides myOverrides = new XmlAttributeOverrides();  
    myOverrides.Add(typeof(Book), "ISBN", myAttributes);  
    XmlSerializer mySerializer =   
    new XmlSerializer(typeof(Book), myOverrides)  
    Book b = new Book();  
    b.ISBN = "123456789"  
    // Creates a StreamWriter to write the XML stream to.  
    StreamWriter writer = new StreamWriter("Book.xml");  
    mySerializer.Serialize(writer, b);  
}  

XML 資料流可能類似下列所示。The XML stream might resemble the following.

<Book>  
    <BookID>123456789</BookID>  
</Book>  

另請參閱See also