XmlElementAttributes.Add(XmlElementAttribute) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為收藏增添 a XmlElementAttribute 。
public:
int Add(System::Xml::Serialization::XmlElementAttribute ^ attribute);
public int Add(System.Xml.Serialization.XmlElementAttribute attribute);
public int Add(System.Xml.Serialization.XmlElementAttribute? attribute);
member this.Add : System.Xml.Serialization.XmlElementAttribute -> int
Public Function Add (attribute As XmlElementAttribute) As Integer
參數
- attribute
- XmlElementAttribute
還有 XmlElementAttribute 一點要補充。
傳回
新加入項目的零基索引。
範例
以下範例建立兩個 XmlElementAttribute 物件,並呼叫 Add 方法將它們加入 XmlElementAttributes。 範例接著將 XmlElementAttributesXmlAttributeOverrides加入一個 ,用來建立 , XmlSerializer 可以序列化該 Transportation 類別的實例。
public XmlSerializer CreateOverrider()
{
// Create XmlAttributes and XmlAttributeOverrides instances.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver =
new XmlAttributeOverrides();
/* Create an XmlElementAttributes to override
the Vehicles property. */
XmlElementAttribute xElement1 =
new XmlElementAttribute(typeof(Truck));
// Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1);
/* Create a second XmlElementAttribute, and
add to the collection. */
XmlElementAttribute xElement2 =
new XmlElementAttribute(typeof(Train));
attrs.XmlElements.Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(typeof(Transportation), "Vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer
(typeof(Transportation), xOver);
return xSer;
}
Public Function CreateOverrider() As XmlSerializer
' Create XmlAttributes and XmlAttributeOverrides instances.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlElementAttributes to override
' the Vehicles property.
Dim xElement1 As New XmlElementAttribute(GetType(Truck))
' Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1)
' Create a second XmlElementAttribute, and
' add to the collection.
Dim xElement2 As New XmlElementAttribute(GetType(Train))
attrs.XmlElements.Add(xElement2)
' Add the XmlAttributes to the XmlAttributeOverrides,
' specifying the member to override.
xOver.Add(GetType(Transportation), "Vehicles", attrs)
' Create the XmlSerializer, and return it.
Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
Return xSer
End Function
End Class