public class Groceries
{
private List<GroceryItem> _groceryList;
public Groceries()
{
this._groceryList = new List<GroceryItem>();
this.Budget = 100M;
}
public List<GroceryItem> GroceryList
{
get { return this._groceryList; }
set { this._groceryList = value; }
}
public decimal Budget { get; set; }
}
Public Class Groceries
Private _groceryList As List(Of GroceryItem)
Private _budget As Decimal
Public Sub New()
Me._groceryList = New List(Of GroceryItem)
Me._budget = 100D
End Sub
Public Property GroceryList() As List(Of GroceryItem)
Get
Return Me._groceryList
End Get
Set(ByVal value As List(Of GroceryItem))
Me._groceryList = value
End Set
End Property
Public Property Budget() As Decimal
Get
Return Me._budget
End Get
Set(ByVal value As Decimal)
Me._budget = value
End Set
End Property
End Class
[XmlIgnore]
public decimal SubTotal
{
get { return this._subTotal; }
set { this._subTotal = value; }
}
<XmlIgnore()> _
Public Property SubTotal() As Decimal
Get
Return Me._subTotal
End Get
Set(ByVal value As Decimal)
Me._subTotal = value
End Set
End Property
Unfortunately, unlike binary serialization in .NET, there are no serialization/deserializatpublic int Count
{
get { return this._count; }
set
{
this._count = value;
this._subTotal = this._count * this._price;
}
}
public decimal Price
{
get { return this._price; }
set
{
this._price = value;
this._subTotal = this._price * this._count;
}
}
[ VB ]
Public Property Count() As Integer
Get
Return Me._count
End Get
Set(ByVal value As Integer)
Me._count = value
Me._subTotal = Me._count * Me._price
End Set
End Property
Public Property Price() As Decimal
Get
Return Me._price
End Get
Set(ByVal value As Decimal)
Me._price = value
Me._subTotal = Me._price * Me._count
End Set
End Property
We have now set up our class to be serialized. What next? Well, we set up our serialization code as demonstrated in the last article. I will not re-post that code here, but the link at the top of the page will take you back, should you need a refresher. I will, however, provide the complete projects (C# and VB) for download.
static void Main(string[] args)
{
Groceries g = new Groceries();
g.Budget = 5000;
g.GroceryList.Add(new GroceryItem("bread", 3, 1.99M));
g.GroceryList.Add(new GroceryItem("milk", 1, 2.99M));
g.GroceryList.Add(new GroceryItem("soup", 5, 1.25M));
SerializeIt<Groceries>(g, "test.xml");
}
[ VB ]
Sub Main()
Dim g As New Groceries
g.Budget = 5000
g.GroceryList.Add(New GroceryItem("bread", 3, 1.99D))
g.GroceryList.Add(New GroceryItem("milk", 1, 2.99D))
g.GroceryList.Add(New GroceryItem("soup", 5, 1.25D))
SerializeIt(Of Groceries)(g, "test.xml")
End Sub
what should we expect to see in our output? Well...
<?xml version="1.0" encoding="utf-8"?>
<Groceries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GroceryList>
<GroceryItem>
<Name>bread</Name>
<Count>3</Count>
<Price>1.99</Price>
</GroceryItem>
<GroceryItem>
<Name>milk</Name>
<Count>1</Count>
<Price>2.99</Price>
</GroceryItem>
<GroceryItem>
<Name>soup</Name>
<Count>5</Count>
<Price>1.25</Price>
</GroceryItem>
</GroceryList>
<Budget>5000</Budget>
</Groceries>
Again, you'll notice that SubTotal was not output due to the use of XmlIgnore. Had we not used XmlIgnore, our output would look like:
<?xml version="1.0" encoding="utf-8"?>
<Groceries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GroceryList>
<GroceryItem>
<Name>bread</Name>
<Count>3</Count>
<Price>1.99</Price>
<SubTotal>5.97</SubTotal>
</GroceryItem>
<GroceryItem>
<Name>milk</Name>
<Count>1</Count>
<Price>2.99</Price>
<SubTotal>2.99</SubTotal>
</GroceryItem>
<GroceryItem>
<Name>soup</Name>
<Count>5</Count>
<Price>1.25</Price>
<SubTotal>6.25</SubTotal>
</GroceryItem>
</GroceryList>
<Budget>5000</Budget>
</Groceries>
We have our SubTotal again!
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GroceryItem" nillable="true" type="GroceryItem" />
<xs:complexType name="GroceryItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Count" type="xs:int" />
<xs:element minOccurs="1" maxOccurs="1" name="Price" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:element name="Groceries" nillable="true" type="Groceries" />
<xs:complexType name="Groceries">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="GroceryList" type="ArrayOfGroceryItem" />
<xs:element minOccurs="1" maxOccurs="1" name="Budget" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfGroceryItem">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="GroceryItem" nillable="true" type="GroceryItem" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented:I appreciate your assitance :)
Is there anything further I need to do ATM?