本文整理匯總了VB.NET中System.Xml.Serialization.XmlElementAttributes類的典型用法代碼示例。如果您正苦於以下問題:VB.NET XmlElementAttributes類的具體用法?VB.NET XmlElementAttributes怎麽用?VB.NET XmlElementAttributes使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了XmlElementAttributes類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Transportation
' 導入命名空間
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Public Class Transportation
' Override these two XmlElementAttributes.
<XmlElement(GetType(Car)), _
XmlElement(GetType(Plane))> _
Public Vehicles As ArrayList
End Class
Public Class Car
Public Name As String
End Class
Public Class Plane
Public Name As String
End Class
Public Class Truck
Public Name As String
End Class
Public Class Train
Public Name As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializeObject("OverrideElement.xml")
End Sub
Public Function CreateOverrider() As XmlSerializer
' Create XmlAtrributes and XmlAttributeOverrides instances.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlElementAttributes object to override
' one of the attributes applied to 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 it 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
Public Sub SerializeObject(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object.
Dim myTransportation As New Transportation()
' Create two new, overriding objects that can be
' inserted into the Vehicles array.
myTransportation.Vehicles = New ArrayList()
Dim myTruck As New Truck()
myTruck.Name = "MyTruck"
Dim myTrain As New Train()
myTrain.Name = "MyTrain"
myTransportation.Vehicles.Add(myTruck)
myTransportation.Vehicles.Add(myTrain)
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myTransportation)
End Sub
End Class