S
Shan1986
Guest
Hi,
I have a class customers and i would like to export my class to xml. I tried following and i get XmlSerializer is not defined error.
code to serialize.
' Create the serializer
Dim serializer As New XmlSerializer(GetType(Customers))
' Serialize the object to an XML file
Using streamWriter As StreamWriter = File.CreateText("CustomerXML.xml")
serializer.Serialize(streamWriter, customerList)
End Using
Imports System.Xml.Serialization
Public Class CustomerManager
Public Shared Function GetAllCustomers() As Customers
Dim customerList As New Customers()
customerList.Add(New Customer("BOTTM", "Bottom-Dollar Markets", "Tsawassen", "BC", "Canada"))
customerList(0).Orders.Add(New Order(1, "BOTTM", #1/4/2004#, #1/11/2004#))
customerList(0).Orders.Add(New Order(2, "BOTTM", #2/9/2004#, #2/16/2004#))
customerList(0).Orders.Add(New Order(3, "BOTTM", #3/2/2004#, #3/7/2004#))
customerList(0).Orders(0).subOrders.Add(New SubOrder(1, "Test", "T.01"))
customerList.Add(New Customer("GOURL", "Gourmet Lanchonetes", "Campinas", "SP", "Brazil"))
customerList(1).Orders.Add(New Order(4, "GOURL", #4/7/2004#, #4/14/2004#))
customerList(1).Orders.Add(New Order(5, "GOURL", #5/1/2004#, #5/14/2004#))
customerList(1).Orders.Add(New Order(6, "GOURL", #6/20/2004#, #6/23/2004#))
customerList(1).Orders(0).subOrders.Add(New SubOrder(4, "Test1", "t.01"))
customerList(1).Orders(1).subOrders.Add(New SubOrder(5, "Test5", "t.015"))
customerList.Add(New Customer("GREAL", "Great Lakes Food Market", "Eugene", "OR", "USA"))
customerList(2).Orders.Add(New Order(7, "GREAL", #7/7/2004#, #7/14/2004#))
customerList(2).Orders.Add(New Order(8, "GREAL", #8/1/2004#, #8/14/2004#))
customerList(2).Orders.Add(New Order(9, "GREAL", #9/20/2004#, #9/23/2004#))
customerList(2).Orders(0).subOrders.Add(New SubOrder(7, "Test7", "T.015"))
Return customerList
End Function
Public Shared Function GetNewCustomer() As Customer
Return New Customer("CustID", "CompanyName", "City", "Region", "Country")
End Function
End Class
Imports System.Xml.Serialization
<Serializable()>
Public Class Customer
Public Sub New(ByVal customerId As String, ByVal companyName As String, ByVal city As String, ByVal region As String, ByVal country As String)
Initialize()
customerIDValue = customerId
companyNameValue = companyName
cityValue = city
regionValue = region
countryValue = country
End Sub
Private Sub Initialize()
ordersValue = New Orders()
End Sub
Private customerIDValue As String
''' <summary>
''' Identifier for the customer
''' </summary>
Public Property CustomerID() As String
Get
Return customerIDValue
End Get
Set(ByVal value As String)
customerIDValue = value
End Set
End Property
Private companyNameValue As String
''' <summary>
''' The name of this customer
''' </summary>
Public Property CompanyName() As String
Get
Return companyNameValue
End Get
Set(ByVal Value As String)
companyNameValue = Value
End Set
End Property
Private cityValue As String
''' <summary>
''' The city where this customer is located
''' </summary>
Public Property City() As String
Get
Return cityValue
End Get
Set(ByVal Value As String)
cityValue = Value
End Set
End Property
Private regionValue As String
''' <summary>
''' The region where this customer is located
''' </summary>
Public Property Region() As String
Get
Return regionValue
End Get
Set(ByVal Value As String)
regionValue = Value
End Set
End Property
Private countryValue As String
''' <summary>
''' The country where this customer is located
''' </summary>
Public Property Country() As String
Get
Return countryValue
End Get
Set(ByVal Value As String)
countryValue = Value
End Set
End Property
Private WithEvents ordersValue As Orders
''' <summary>
''' The orders for this customer
''' </summary>
Public Property Orders() As Orders
Get
Return ordersValue
End Get
Set(ByVal value As Orders)
ordersValue = value
End Set
End Property
Private Sub ordersValue_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles ordersValue.AddingNew
e.NewObject = New Order(999, Me.CustomerID, Date.Today, DateAdd(DateInterval.Day, 30, Date.Today))
End Sub
End Class
Public Class Customers
Inherits System.ComponentModel.BindingList(Of Customer)
End Class
thanks
Continue reading...
I have a class customers and i would like to export my class to xml. I tried following and i get XmlSerializer is not defined error.
code to serialize.
' Create the serializer
Dim serializer As New XmlSerializer(GetType(Customers))
' Serialize the object to an XML file
Using streamWriter As StreamWriter = File.CreateText("CustomerXML.xml")
serializer.Serialize(streamWriter, customerList)
End Using
Imports System.Xml.Serialization
Public Class CustomerManager
Public Shared Function GetAllCustomers() As Customers
Dim customerList As New Customers()
customerList.Add(New Customer("BOTTM", "Bottom-Dollar Markets", "Tsawassen", "BC", "Canada"))
customerList(0).Orders.Add(New Order(1, "BOTTM", #1/4/2004#, #1/11/2004#))
customerList(0).Orders.Add(New Order(2, "BOTTM", #2/9/2004#, #2/16/2004#))
customerList(0).Orders.Add(New Order(3, "BOTTM", #3/2/2004#, #3/7/2004#))
customerList(0).Orders(0).subOrders.Add(New SubOrder(1, "Test", "T.01"))
customerList.Add(New Customer("GOURL", "Gourmet Lanchonetes", "Campinas", "SP", "Brazil"))
customerList(1).Orders.Add(New Order(4, "GOURL", #4/7/2004#, #4/14/2004#))
customerList(1).Orders.Add(New Order(5, "GOURL", #5/1/2004#, #5/14/2004#))
customerList(1).Orders.Add(New Order(6, "GOURL", #6/20/2004#, #6/23/2004#))
customerList(1).Orders(0).subOrders.Add(New SubOrder(4, "Test1", "t.01"))
customerList(1).Orders(1).subOrders.Add(New SubOrder(5, "Test5", "t.015"))
customerList.Add(New Customer("GREAL", "Great Lakes Food Market", "Eugene", "OR", "USA"))
customerList(2).Orders.Add(New Order(7, "GREAL", #7/7/2004#, #7/14/2004#))
customerList(2).Orders.Add(New Order(8, "GREAL", #8/1/2004#, #8/14/2004#))
customerList(2).Orders.Add(New Order(9, "GREAL", #9/20/2004#, #9/23/2004#))
customerList(2).Orders(0).subOrders.Add(New SubOrder(7, "Test7", "T.015"))
Return customerList
End Function
Public Shared Function GetNewCustomer() As Customer
Return New Customer("CustID", "CompanyName", "City", "Region", "Country")
End Function
End Class
Imports System.Xml.Serialization
<Serializable()>
Public Class Customer
Public Sub New(ByVal customerId As String, ByVal companyName As String, ByVal city As String, ByVal region As String, ByVal country As String)
Initialize()
customerIDValue = customerId
companyNameValue = companyName
cityValue = city
regionValue = region
countryValue = country
End Sub
Private Sub Initialize()
ordersValue = New Orders()
End Sub
Private customerIDValue As String
''' <summary>
''' Identifier for the customer
''' </summary>
Public Property CustomerID() As String
Get
Return customerIDValue
End Get
Set(ByVal value As String)
customerIDValue = value
End Set
End Property
Private companyNameValue As String
''' <summary>
''' The name of this customer
''' </summary>
Public Property CompanyName() As String
Get
Return companyNameValue
End Get
Set(ByVal Value As String)
companyNameValue = Value
End Set
End Property
Private cityValue As String
''' <summary>
''' The city where this customer is located
''' </summary>
Public Property City() As String
Get
Return cityValue
End Get
Set(ByVal Value As String)
cityValue = Value
End Set
End Property
Private regionValue As String
''' <summary>
''' The region where this customer is located
''' </summary>
Public Property Region() As String
Get
Return regionValue
End Get
Set(ByVal Value As String)
regionValue = Value
End Set
End Property
Private countryValue As String
''' <summary>
''' The country where this customer is located
''' </summary>
Public Property Country() As String
Get
Return countryValue
End Get
Set(ByVal Value As String)
countryValue = Value
End Set
End Property
Private WithEvents ordersValue As Orders
''' <summary>
''' The orders for this customer
''' </summary>
Public Property Orders() As Orders
Get
Return ordersValue
End Get
Set(ByVal value As Orders)
ordersValue = value
End Set
End Property
Private Sub ordersValue_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles ordersValue.AddingNew
e.NewObject = New Order(999, Me.CustomerID, Date.Today, DateAdd(DateInterval.Day, 30, Date.Today))
End Sub
End Class
Public Class Customers
Inherits System.ComponentModel.BindingList(Of Customer)
End Class
thanks
Continue reading...