VB.Net/File Directory/Serialize Object
Содержание
Save Serializable Object to binary file: create a sequential-access file
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Public Class MainClass
Public Shared Sub Main()
Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim output As FileStream
Dim fileName As String = "test.dat"
Dim employee As Employee = New Employee("First Name", "Last Name")
Try
output = New FileStream(fileName,FileMode.OpenOrCreate, FileAccess.Write)
formatter.Serialize(output, employee)
output.Close()
Catch fileException As FileNotFoundException
Console.WriteLine("File Does Not Exits")
Catch serializableException As SerializationException
Console.WriteLine("Error Writing to File")
Catch formattingException As FormatException
Console.WriteLine("Invalid Format")
Catch e As IOException
Console.WriteLine("Cannot close file")
End Try
End Sub
End Class
<Serializable()> Public Class Employee
Private firstName, lastName As String
Public Sub New(ByVal first As String, ByVal last As String)
firstName = first
lastName = last
End Sub
Public Overrides Function ToString() As String
Return firstName & " " & lastName
End Function
End Class
Serializable Person Object
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Public Class MainClass
Public Shared Sub Main()
Dim Bob As New SerializablePerson("Bob", 34, 5.25)
Dim fs As New FileStream("c:\bob.dat", FileMode.Create)
Dim f As System.Runtime.Serialization.IFormatter
f = New SoapFormatter()
"f = New BinaryFormatter()
f.Serialize(fs, Bob)
fs.Close()
Bob = Nothing
fs = New FileStream("c:\bob.dat", FileMode.Open)
Bob = CType(f.Deserialize(fs), SerializablePerson)
System.Console.WriteLine(Bob.Name)
fs.Close()
End Sub
End Class
<Serializable()> Public Class SerializablePerson
Public Name As String
Public Age As Integer
Public Height As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Age As String, _
ByVal Height As String)
Me.Name = Name
Me.Age = Age
Me.Height = Height
End Sub
End Class
Serialize Data to Binary and XML at the same time
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.Serialization.Formatters.Binary
public class MainClass
Shared Sub Main()
"Create Connection object
Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
Dim sql As String = "SELECT * FROM Employee"
Dim fs As New FileStream("Employee.bin",FileMode.Create)
Try
" Create Data Adapter
Dim da As New SqlDataAdapter
da.SelectCommand = New SqlCommand(sql, thisConnection)
" Create and fill Dataset
Dim ds As New DataSet
da.Fill(ds, "Employee")
" Extract DataSet to XML file
ds.WriteXml("Employee.xml")
" Create binary formatter
Dim bf As New BinaryFormatter()
" Specify binary serialization for dataset
ds.RemotingFormat = SerializationFormat.Binary
" Output dataset
bf.Serialize(fs, ds)
Catch ex As SqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
fs.Close()
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try
End Sub
End Class
Serialize Object to XML Stream and output to screen
Imports System
Imports System.Xml
Imports System.Xml.Serialization
Public Class MainClass
Shared Sub Main( )
Dim serialize As XmlSerializer = _
New XmlSerializer(GetType(Order))
Dim MyMovieOrder As Order = _
New Order("Name", 101, 10)
serialize.Serialize(Console.Out, MyMovieOrder)
Console.Out.WriteLine()
End Sub
End Class
Public Class Order
Public name As String
Public id As Integer
Public quantity As Integer
Public Sub New()
End Sub
Public Sub New(ByVal name As String, _
ByVal id As Integer, _
ByVal quantity As Integer)
Me.name = name
Me.id = id
Me.quantity = quantity
End Sub
End Class
Serialize to Xml
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class MainClass
Public Shared Sub Main()
Dim roster = New EmployeeCollection(DateTime.Now)
Dim employees = New Employee() _
{New Employee With {.Id = 1, .Name = "A", _
.Title = "Coder", _
.HireDate = DateTime.Now, _
.HourlyRate = 100.0}, _
New Employee With {.Id = 4, .Name = "B", _
.Title = "Coder", _
.HireDate = DateTime.Now, _
.HourlyRate = 100.75}}
roster.Employees = employees
Dim serializer As New XmlSerializer(GetType(EmployeeCollection))
Dim fs As New FileStream("EmployeeCollection.xml", FileMode.Create)
serializer.Serialize(fs, roster)
fs.Close()
roster = Nothing
fs = New FileStream("EmployeeCollection.xml", FileMode.Open)
roster = DirectCast(serializer.Deserialize(fs), EmployeeCollection)
serializer.Serialize(Console.Out, roster)
End Sub
End Class
<XmlRoot("EmployeeCollection")> _
Public Class EmployeeCollection
<XmlElement(ElementName:="LastUpdated", datatype:="date")> _
Public LastUpdated As DateTime
<XmlArray("Employees"), XmlArrayItem("Employee")> _
Public Employees As Employee()
Public Sub New()
End Sub
Public Sub New(ByVal update As DateTime)
Me.LastUpdated = update
End Sub
End Class
Public Class Employee
<XmlElement("Name")> _
Public Name As String = String.Empty
<XmlElement("Title")> _
Public Title As String = String.Empty
<XmlElement(ElementName:="HireDate", datatype:="date")> _
Public HireDate As DateTime = Date.MinValue
<XmlElement("HourlyRate")> _
Public HourlyRate As Decimal = 0
<XmlAttribute(AttributeName:="id", DataType:="integer")> _
Public Id As String = String.Empty
Public Sub New()
End Sub
Public Sub New(ByVal employeName As String, ByVal employeeTitle As String, ByVal employeeHireDate As DateTime, ByVal employeeHourlyRate As Decimal)
Me.Name = employeName
Me.Title = employeeTitle
Me.HireDate = employeeHireDate
Me.HourlyRate = employeeHourlyRate
End Sub
End Class
Simple Serializable Person Object
Imports System.IO
Public Class MainClass
Public Shared Sub Main()
Dim Bob As New Person("Bob", 34, 5.25)
Bob.SaveToFile("c:\a.bin")
Bob = Nothing
Bob = Person.LoadFromFile("c:\a.bin")
System.Console.WriteLine(Bob.Name)
End Sub
End Class
Public Class Person
Public Name As String
Public Age As Integer
Public Height As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Age As String, ByVal Height As String)
Me.Name = Name
Me.Age = Age
Me.Height = Height
End Sub
Public Sub SaveToFile(ByVal Filename As String)
Dim fs As New FileStream(Filename, FileMode.Create)
Dim w As New BinaryWriter(fs)
w.Write(Name)
w.Write(Age)
w.Write(Height)
w.Close()
End Sub
Public Shared Function LoadFromFile(ByVal Filename As String) As Person
Dim fs As New FileStream(Filename, FileMode.Open)
Dim r As New BinaryReader(fs)
Dim NewPerson As New Person()
NewPerson.Name = r.ReadString()
NewPerson.Age = r.ReadInt32()
NewPerson.Height = r.ReadInt32()
r.Close()
Return NewPerson
End Function
End Class