本文整理汇总了VB.NET中System.Xml.Serialization.XmlAttributes.XmlEnum属性的典型用法代码示例。如果您正苦于以下问题:VB.NET XmlAttributes.XmlEnum属性的具体用法?VB.NET XmlAttributes.XmlEnum怎么用?VB.NET XmlAttributes.XmlEnum使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Xml.Serialization.XmlAttributes
的用法示例。
在下文中一共展示了XmlAttributes.XmlEnum属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Food
' 导入命名空间
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Food
Public Type As FoodType
End Class
Public Enum FoodType
' Subsequent code overrides these enumerations.
Low
High
End Enum
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("OverrideEnum.xml")
test.DeserializeObject("OverrideEnum.xml")
End Sub
' Return an XmlSerializer used for overriding.
Public Function CreateOverrider() As XmlSerializer
' Create the XmlAttributeOverrides and XmlAttributes objects.
Dim xOver As New XmlAttributeOverrides()
Dim xAttrs As New XmlAttributes()
' Add an XmlEnumAttribute for the FoodType.Low enumeration.
Dim xEnum As New XmlEnumAttribute()
xEnum.Name = "Cold"
xAttrs.XmlEnum = xEnum
xOver.Add(GetType(FoodType), "Low", xAttrs)
' Add an XmlEnumAttribute for the FoodType.High enumeration.
xAttrs = New XmlAttributes()
xEnum = New XmlEnumAttribute()
xEnum.Name = "Hot"
xAttrs.XmlEnum = xEnum
xOver.Add(GetType(FoodType), "High", xAttrs)
' Create the XmlSerializer, and return it.
Return New XmlSerializer(GetType(Food), xOver)
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an instance of the XmlSerializer class.
Dim mySerializer As XmlSerializer = CreateOverrider()
' Writing the file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Create an instance of the class that will be serialized.
Dim myFood As New Food()
' Set the object properties.
myFood.Type = FoodType.High
' Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myFood)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim mySerializer As XmlSerializer = CreateOverrider()
Dim fs As New FileStream(filename, FileMode.Open)
Dim myFood As Food = CType(mySerializer.Deserialize(fs), Food)
Console.WriteLine(myFood.Type)
End Sub
End Class