本文整理汇总了VB.NET中System.Xml.Serialization.XmlSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:VB.NET XmlSerializer.Serialize方法的具体用法?VB.NET XmlSerializer.Serialize怎么用?VB.NET XmlSerializer.Serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSerializer
的用法示例。
在下文中一共展示了XmlSerializer.Serialize方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With XmlTextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlTextWriter using a FileStream.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New XmlTextWriter(fs, Encoding.Unicode)
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
示例2: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With XmlTextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create an XmlTextWriter using a FileStream.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New XmlTextWriter(fs, New UTF8Encoding())
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
End Class
示例3: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With TextWriter")
' Create an XmlSerializer instance using the type.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
i.ItemName = "Widget"
i.Description = "Regular Widget"
i.Quantity = 10
i.UnitPrice = CDec(2.3)
i.Calculate()
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create a StreamWriter to write with.
Dim writer As New StreamWriter(filename)
' Serialize using the object using the TextWriter
' and namespaces.
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
End Class
示例4: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With TextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create a StreamWriter to write with. First create a FileStream
' object, and create the StreamWriter specifying an Encoding to use.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New StreamWriter(fs, New UTF8Encoding())
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
示例5: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With Stream")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create a FileStream to write with.
Dim writer As New FileStream(filename, FileMode.Create)
' Serialize the object, and close the TextWriter
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
示例6: OrderedItem
' 导入命名空间
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
t.DeserializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With Stream")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two prefix-namespace pairs.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create a FileStream to write with.
Dim writer As New FileStream(filename, FileMode.Create)
' Serialize the object, and close the TextWriter
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
Private Sub DeserializeObject(ByVal filename As String)
Console.WriteLine("Reading with Stream")
' Create an instance of the XmlSerializer.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' Writing the file requires a Stream.
Dim reader As New FileStream(filename, FileMode.Open)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Use the Deserialize method to restore the object's state
' using data from the XML document.
i = CType(serializer.Deserialize(reader), OrderedItem)
' Write out the properties of the object.
Console.Write(i.ItemName & ControlChars.Tab & _
i.Description & ControlChars.Tab & _
i.UnitPrice & ControlChars.Tab & _
i.Quantity & ControlChars.Tab & _
i.LineTotal)
End Sub
End Class
示例7: MainClass
' 导入命名空间
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Public Class MainClass
Public Shared Sub Main()
Dim serialize As XmlSerializer = New XmlSerializer(GetType(Product))
Dim MyMovieOrder As Product = New Product("PC", 101, 10)
serialize.Serialize(Console.Out, MyMovieOrder)
End Sub
End Class
Public Class Product
<XmlElementAttribute("Name")> Public name As String
<XmlAttributeAttribute("ID")> Public filmId As Integer
<XmlAttributeAttribute("count")> Public quantity As Integer
Public Sub New()
End Sub
Public Sub New(ByVal name As String, _
ByVal filmId As Integer, _
ByVal quantity As Integer)
Me.name = name
Me.filmId = filmId
Me.quantity = quantity
End Sub
End Class