如何:指定 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 services 需要的基本信息相同(略有差异),因此您或许希望用同一组类生成多个 XML 流。You might want to do this because two different XML Web services require the same basic information, with only slight differences. 例如,假设有两个处理书籍订单的 XML Web services,它们都需要 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 services,以上行为与预期相同。For the first XML Web service, this is as expected. 但如果要将 XML 流发送至第二个 XML Web services,则必须重写序列化,以便使标记的元素名称采用 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. 向通过 XmlElementAttributeXmlElements 属性访问的集合中添加 XmlAttributes 对象。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