Imports System Imports System.Web Imports System.Web.UI Namespace Controls Public Class BookView Inherits Control @# need somewhere to store the book... Dim m_Book As Book Public Property Book() As Book Get Return m_Book End Get Set m_Book = Value End Set End Property End Class End Namespace |
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) @# do we have a book? If m_Book Is Nothing Then @# render something... writer.Write("Nothing to do!") Else 如果我们得到了一个Book对象,那我们就可以象下面这样做了: @# draw the name of the book... writer.Write("Type: " & book.GetType().ToString & "<br>") writer.Write("Name: " & book.Name & "<br>") writer.Write("ISBN: " & book.ISBN & "<br>") End If End Sub |
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.vb" Inherits="Serialization.WebForm1"%> <%@ Register TagPrefix="mycontrols" Namespace="Serialization.Controls" %> |
<form id="WebForm1" method="post" runat="server"> <textarea runat="server" id="xmlarea" cols="60" rows="15"></textarea> <br> <input runat="server" type="submit" value="Turn XML into an object!"> </form> <mycontrols:bookview runat="server" id="bookview"></mycontrols:bookview> |
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents xmlarea As System.Web.UI.HtmlControls.HtmlTextArea Protected WithEvents bookview As Controls.BookView |
Protected Sub WebForm1_Load(ByVal Sender As System.Object, _ ByVal e As System.EventArgs) If Not IsPostback Then @# Evals true first time browser hits the page Else @# try and turn the xml back into an object... Dim reader As New StringReader(xmlarea.Value) |
@# create a serializer... Dim MyBook As New Book() Dim serializer As New XmlSerializer(MyBook.GetType()) 最后我们用Try…Catch块来做Deserialize。因为我们不能保证用户一定会给我们一个正确的xml块,所以我们需要确保不会出错。 @# try and read it... Try @# turn it into a book... bookview.Book = CType(serializer.Deserialize(reader), Book) Catch End Try End If End Sub |
Imports System Public Class Author Private m_Name As String Public Property Name () As String Get Return m_name End Get Set m_name = value End Set End Property End Class |
Private m_Name As String Private m_ISBN As String Private m_Authors As New Hashtable() |
@# a method to add an author... Public Function AddAuthor(ByVal Name As String) As Author @# create a new author... Dim NewAuthor As New Author() NewAuthor.Name = Name @# add it to the collection... m_Authors.Add(Name, NewAuthor) End Function |
@# create an instance of a book... Dim MyBook As New Book() MyBook.Name = "Beginning E-commerce" MyBook.ISBN = "1861003986" MyBook.AddAuthor("Matthew Reynolds") |
Public Property Authors() As Author() Get @# create a new author array... Dim AuthorArray(m_authors.Count) As Author Dim Author As Author, n As Integer For Each author In m_Authors.Values AuthorArray(n) = Author n += 1 Next @# return the array... Return AuthorArray End Get Set @# reset the authors we@#ve already got (i.e. what @# we are given is the definitive list.) m_Authors.Clear() @# go through the authors we have... Dim Author As Author For Each Author In Value m_Authors.Add(Author.Name, Author) Next End Set End Property |
@# draw the name of the book... writer.Write("Type: " & book.GetType().ToString & "<br>") writer.Write("Name: " & book.Name & "<br>") writer.Write("ISBN: " & book.ISBN & "<br>") @# loop through the authors... Dim Author As Author For Each Author In m_Book.Authors writer.Write("Author: " & Author.Name & "<br>") Next |