home *** CD-ROM | disk | FTP | other *** search
- Imports System.IO
- Imports System.Xml.Serialization
-
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestXmlSerializer()
- 'TestXmlSerializer2()
- 'TestXmlSerializer3()
- 'TestXmlSerializer4()
- 'TestDeserializationEvents()
- 'TestSerializationOverriding()
- 'TestSerializationOverriding2()
-
- ' These statements are usuful when running inside Visual Studio.NET
- Console.WriteLine("")
- Console.WriteLine(">>> Press Enter to terminate the program <<<")
- Console.ReadLine()
-
- End Sub
-
- ' this procedure tests the XmlSerializer
-
- Sub TestXmlSerializer()
- ' Create an XMLSerializer object for the Customer class.
- Dim ser As New XmlSerializer(GetType(Customer))
- ' Create a Publisher object.
- Dim cust As New Customer(1, "Joe Doe", Nothing, "New York")
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
- ' Serialize the object to the stream and close it
- ser.Serialize(fs, cust)
- fs.Close()
-
- ' Reopen the stream.
- Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim cust2 As Customer = CType(ser.Deserialize(fs2), Customer)
- fs2.Close()
- ' check object properties
- Console.WriteLine(cust2.Name & ", " & cust2.City) ' => Joe Doe, New York
- End Sub
-
- ' this procedure tests the Publisher2 class, which contains attributes
-
- Sub TestXmlSerializer2()
- ' Create an XMLSerializer object for the Customer class.
- Dim ser As New XmlSerializer(GetType(Customer2))
- ' Create a Publisher object.
- Dim cust As New Customer2(1, "Joe Doe", Nothing, "New York")
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
- ' Serialize the object to the stream and close it
- ser.Serialize(fs, cust)
- fs.Close()
-
- ' Reopen the stream.
- Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim cust2 As Customer2 = CType(ser.Deserialize(fs2), Customer2)
- fs2.Close()
- ' check object properties
- Console.WriteLine(cust2.Name & ", " & cust2.City) ' => Joe Doe, New York
- End Sub
-
- ' this procedure tests the serialization of nested objects.
-
- Sub TestXmlSerializer3()
- ' Create an XMLSerializer object for the Customer class.
- Dim ser As New XmlSerializer(GetType(Customer4))
- ' Create a Publisher object.
- Dim cust As New Customer4(1, "Joe Doe", Nothing, "New York")
- ' add some orders
- cust.Orders(0) = New Order(1, #1/2/2001#, 123.5)
- cust.Orders(1) = New Order(2, #4/8/2001#, 450.8)
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
- ' Serialize the object to the stream and close it
- ser.Serialize(fs, cust)
- fs.Close()
-
- ' Reopen the stream.
- Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim cust2 As Customer4 = CType(ser.Deserialize(fs2), Customer4)
- fs2.Close()
-
- ' check object properties
- Console.WriteLine(cust2.Name & ", " & cust2.City) ' => Joe Doe, New York
- Console.WriteLine(cust2.Orders(0).Date) ' => 1/2/2001 12:00:00 AM
- End Sub
-
- ' this procedure tests serialization of nested objects with namespaces
-
- Sub TestXmlSerializer4()
- ' Create an XMLSerializer object for the Customer class.
- Dim ser As New XmlSerializer(GetType(Customer4))
- ' Create a Publisher object.
- Dim cust As New Customer4(1, "Joe Doe", Nothing, "New York")
- ' add some orders
- cust.Orders(0) = New Order(1, #1/2/2001#, 123.5)
- cust.Orders(1) = New Order(2, #4/8/2001#, 450.8)
-
- Dim ns As New XmlSerializerNamespaces()
- ns.Add("Customer4", "http://www.vb2themax.com")
- ns.Add("Order", "http://www.wintellect.com")
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
- ' Serialize the object to the stream, enforcing the specified namespaces.
- ser.Serialize(fs, cust, ns)
- fs.Close()
-
- ' Reopen the stream, using the same XmlSerializer.
- Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim cust2 As Customer4 = CType(ser.Deserialize(fs2), Customer4)
- fs2.Close()
-
- ' check object properties
- Console.WriteLine(cust2.Name & ", " & cust2.City) ' => Joe Doe, New York
- Console.WriteLine(cust2.Orders(0).Date) ' => 1/2/2001 12:00:00 AM
- End Sub
-
- ' this procedure tests deserialization events.
-
- Sub TestDeserializationEvents()
- ' Create an XMLSerializer object for the Publisher class.
- Dim ser As New XmlSerializer(GetType(Customer))
- ' dynamically create the event handler
- AddHandler ser.UnknownElement, AddressOf Deserialization_UnknownElement
-
- ' Reopen the stream.
-
- Dim fs2 As New FileStream("c:\customer2.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim cust2 As Customer = CType(ser.Deserialize(fs2), Customer)
- fs2.Close()
-
- ' check object properties
- Console.WriteLine(cust2.Name & ", " & cust2.City) ' => Joe Doe, New York
- End Sub
-
- ' this event is raised when the XMLSerializer founds an unknown XML element
-
- Sub Deserialization_UnknownElement(ByVal sender As Object, ByVal e As XmlElementEventArgs)
- ' cast the element to a Customer object
- Dim cust As Customer = CType(e.ObjectBeingDeserialized, Customer)
- ' there are two cases: we've found a FirstName or LastName XML element
- If e.Element.Name = "FirstName" Then
- ' if it is a FirstName, prefix the current Name property.
- cust.Name = e.Element.InnerXml
- ElseIf e.Element.Name = "LastName" Then
- ' if it is a Lastname element, append to current Name property.
- cust.Name += " " & e.Element.InnerXml
- End If
- End Sub
-
- ' this procedure tests XML serialization overriding
-
- Sub TestSerializationOverriding()
- ' step 1: create an XmlAttributeOverrides object
- Dim xmlAttrOver As New XmlAttributeOverrides()
-
- ' (A) add the XmlRootAttribute("EmployeeList") attribute
-
- ' step A2: create the XmlAttributes object.
- Dim xmlAttrs As New XmlAttributes()
- ' step A3: create and initialize the XmlRootAttribute
- Dim attr1 As New XmlRootAttribute("EmployeeList")
- ' step A4: assign it to the correct property of the XmlAttributes object
- xmlAttrs.XmlRoot = attr1
- ' step A5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' When you add an XmlRootAttribute you specify only 2 arguments:
- ' the 1st argument is the class that contains the overridden member
- ' the 2nd argument is the XmlAttributes object that defines how the member is overridden.
- xmlAttrOver.Add(GetType(AddressBook), xmlAttrs)
-
- ' (B) add the XmlArray("Employees") and XmlArrayItem("Employee") attributes.
-
- ' step B2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
-
- ' step B3: create and initialize the XmlArrayAttribute object.
- Dim attr2 As New XmlArrayAttribute("Employees")
- ' step B4: assign it to the correct property of the XmlAttributes object
- xmlAttrs.XmlArray = attr2
- ' again step B3/B4, this time for the XmlArrayItemAttribute object.
- ' (Note that you can do both creation and assignment in one step.)
- xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("Employee"))
- ' step B5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' the 1st argument is the class that contains the overridden member
- ' the 2nd argument is the member being overridden
- ' the 3rd argument is the XmlAttributes object that defines how the member is overridden.
- xmlAttrOver.Add(GetType(AddressBook), "Contacts", xmlAttrs)
-
- ' (C) add the XmlAttributeAttribute("ID") attribute
-
- ' step C2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step C3/C4: create and initialize the XmlArrayAttribute object
- ' and assign it to the
- xmlAttrs.XmlAttribute = New XmlAttributeAttribute("id")
- ' step C5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' (arguments are as in step B5)
- xmlAttrOver.Add(GetType(Person), "ID", xmlAttrs)
-
- ' (D) add the XmlElement("Phone") attribute
-
- ' step D2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step D3/D4: create and initialize the XmlElementAttribute object
- ' and add it to the XmlAttributes's XmlElements collection
- xmlAttrs.XmlElements.Add(New XmlElementAttribute("Phone"))
- ' step D5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' (arguments are as in step B5)
- xmlAttrOver.Add(GetType(Person), "PhoneNumber", xmlAttrs)
-
- ' Step 6: create the XmlSerializer that eses the XmlAttributeOverrides.
- Dim ser As New XmlSerializer(GetType(AddressBook), xmlAttrOver)
-
- '---- create an object tree.
- Dim addrBook As New AddressBook()
- addrBook.Name = "My Employee Book" ' Name of this address book
- ReDim addrBook.Contacts(1) ' We expect two Persons.
-
- Dim p0 As New Person() ' First Person
- p0.ID = 1
- p0.Name = "Joe Doe"
- p0.PhoneNumber = "234-456-6789"
- Dim p1 As New Person() ' Second Person
- p1.ID = 2
- p1.Name = "Robert Smith"
- p1.PhoneNumber = "234-987-6543"
- addrBook.Contacts(0) = p0 ' Store into the Contacts array.
- addrBook.Contacts(1) = p1
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\addrbook.xml", FileMode.Create)
- ' Serialize the object to the stream and close it
- ser.Serialize(fs, addrBook)
- fs.Close()
-
- ' Reopen the stream.
- Dim fs2 As New FileStream("c:\addrbook.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim addrBook2 As AddressBook = CType(ser.Deserialize(fs2), AddressBook)
- fs2.Close()
-
- ' check object properties
- Dim p As Person = CType(addrBook.Contacts(0), Person)
- Console.WriteLine(p.ID.ToString & " - " & p.Name & " " & p.PhoneNumber)
- p = CType(addrBook.Contacts(1), Person)
- Console.WriteLine(p.ID.ToString & " - " & p.Name & " " & p.PhoneNumber)
-
- End Sub
-
- ' this procedure tests xmlserializer overriding with inherited classes
-
- Sub TestSerializationOverriding2()
- ' step 1: create an XmlAttributeOverrides object
- Dim xmlAttrOver As New XmlAttributeOverrides()
-
- ' (A) add the XmlRootAttribute("EmployeeList") attribute
-
- ' step A2: create the XmlAttributes object.
- Dim xmlAttrs As New XmlAttributes()
- ' step A3: create and initialize the XmlRootAttribute
- Dim attr1 As New XmlRootAttribute("EmployeeList")
- ' step A4: assign it to the correct property of the XmlAttributes object
- xmlAttrs.XmlRoot = attr1
- ' step A5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' When you add an XmlRootAttribute you specify only 2 arguments:
- ' the 1st argument is the class that contains the overridden member
- ' the 2nd argument is the XmlAttributes object that defines how the member is overridden.
- xmlAttrOver.Add(GetType(AddressBook), xmlAttrs)
-
- ' (B) add the XmlArray("Employees") and XmlArrayItem("Employee") attributes.
-
- ' step B2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
-
- ' step B3/B4: create and initialize the XmlArrayAttribute object.
- ' and assign it to the correct property of the XmlAttributes object
- xmlAttrs.XmlArray = New XmlArrayAttribute("Employees")
- ' again step B3/B4, this time for the two XmlArrayItemAttribute objects.
- xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("Employee", GetType(Employee)))
- xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("CandidateEmployee", GetType(CandidateEmployee)))
-
- ' step B5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' the 1st argument is the class that contains the overridden member
- ' the 2nd argument is the member being overridden
- ' the 3rd argument is the XmlAttributes object that defines how the member is overridden.
- xmlAttrOver.Add(GetType(AddressBook), "Contacts", xmlAttrs)
-
- ' (C) add the XmlAttributeAttribute("ID") attribute
-
- ' step C2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step C3/C4: create and initialize the XmlAttribute object
- ' and assign it to the correct property of XmlAttributes object
- xmlAttrs.XmlAttribute = New XmlAttributeAttribute("id")
- ' step C5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' (arguments are as in step B5)
- xmlAttrOver.Add(GetType(Person), "ID", xmlAttrs)
-
- ' (D) add the XmlElement("Phone") attribute
-
- ' step D2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step D3/D4: create and initialize the XmlElementAttribute object
- ' and add it to the XmlAttributes's XmlElements collection
- xmlAttrs.XmlElements.Add(New XmlElementAttribute("Phone"))
- ' step D5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- ' (arguments are as in step B5)
- xmlAttrOver.Add(GetType(Person), "PhoneNumber", xmlAttrs)
-
- ' (E) add the XmlAttribute() attribute to the SSN element in Employee class
-
- ' step E2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step E3/E4: create and initialize the XmlAttribute object
- ' and assign it to the correct property of XmlAttributes object
- xmlAttrs.XmlAttribute = New XmlAttributeAttribute("SSN")
- ' step E5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- xmlAttrOver.Add(GetType(Employee), "SSN", xmlAttrs)
-
- ' (F) add the XmlIgnore() attribute to the HireDate element in Employee class
-
- ' step F2: create a new XmlAttributes object.
- xmlAttrs = New XmlAttributes()
- ' step F3/F4: note that you don't actually create an XmlIgnoreAttribute object;
- ' as for all those attribute whose presence is enough to affect the serialization behavior,
- ' in this case you just set the corresponding XmlIgnore property to True.
- xmlAttrs.XmlIgnore = True
- ' step F5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides
- xmlAttrOver.Add(GetType(Employee), "HireDate", xmlAttrs)
-
- ' Step 6: create the XmlSerializer that eses the XmlAttributeOverrides.
- Dim ser As New XmlSerializer(GetType(AddressBook), xmlAttrOver)
-
- '---- create an object tree.
- Dim addrBook As New AddressBook()
- addrBook.Name = "My Address Book" ' Name of this address book
- ReDim addrBook.Contacts(1) ' We expect two Employee.
-
- Dim e0 As New Employee() ' First, an Employee
- e0.Name = "Joe Doe"
- e0.SSN = "111-222-3333"
- e0.PhoneNumber = "234-456-6789"
- Dim e1 As New CandidateEmployee() ' Second, a CandidateEmployee
- e1.ID = 2
- e1.Name = "Robert Smith"
- e1.PhoneNumber = "234-987-6543"
- e1.InterviewDate = #2/8/2001#
- addrBook.Contacts(0) = e0 ' Store into the Contacts array.
- addrBook.Contacts(1) = e1
-
- ' Open the destination file.
- Dim fs As New FileStream("c:\addrbook.xml", FileMode.Create)
- ' Serialize the object to the stream and close it
- ser.Serialize(fs, addrBook)
- fs.Close()
-
- ' Reopen the stream.
- Dim fs2 As New FileStream("c:\addrbook.xml", FileMode.Open)
- ' Deserialize the file into another Customer object, and close the stream.
- Dim addrBook2 As AddressBook = CType(ser.Deserialize(fs2), AddressBook)
- fs2.Close()
-
- ' check object properties
- Dim e As Employee = CType(addrBook.Contacts(0), Employee)
- Console.WriteLine(e.Name & " " & e.PhoneNumber & " " & e.SSN)
- Dim ce As CandidateEmployee = CType(addrBook.Contacts(1), CandidateEmployee)
- Console.WriteLine(ce.Name & " " & ce.PhoneNumber & " " & ce.InterviewDate)
-
- End Sub
-
- End Module
-