Programming 1: Lab 11 v1.0
Programming 1: Lab 11 v1.0
Programming 1: Lab 11 v1.0
Lab 11
v1.0
XmlSerializer Class
https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.7.2
XmlAttribute Class
https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlattribute?view=netframework-4.7.2
XmlElement Class
https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlelement?view=netframework-4.7.2
FileStream Class
https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.7.2
XmlAttributes.XmlRoot Property
https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributes.xmlroot?view=netframework-4.7.2
XmlAttributes.XmlIgnore Property
https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlelement?view=netframework-4.7.2
XmlSerializer.UnknownNode Event
https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.unknownnode?view=netframework-4.7.2
XmlSerializer.UnknownAttribute Event
https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.unknownattribute?view=netframework-4.7.2
Nullable types
https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.7.2
LINQ to XML
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-queries-linq-to-xml
2. Practice .......................................................................................................................................................... 15
<?xml version="1.0"?>
<purchaseOrders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PurchaseOrder>
<shipTo name="William A. Klein">
<street>2231 Huntz Lane</street>
<city>Cambridge</city>
<state>Massachusetts(MA)</state>
<zip>02141</zip>
</shipTo>
<orderDate>2019. január 8., kedd</orderDate>
<items>
<OrderedItem>
<itemName>cup</itemName>
<unitPrice>35.12</unitPrice>
<quantity>10</quantity>
<lineTotal>351.20</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>cup</itemName>
<unitPrice>35.12</unitPrice>
<quantity>10</quantity>
<lineTotal>351.20</lineTotal>
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 3
</OrderedItem>
<OrderedItem>
<itemName>shovel</itemName>
<unitPrice>40.29</unitPrice>
<quantity>1</quantity>
<lineTotal>40.29</lineTotal>
</OrderedItem>
</items>
<subTotal>742.69</subTotal>
<shipCost>12.5</shipCost>
<totalCost>755.19</totalCost>
</PurchaseOrder>
<PurchaseOrder>
<shipTo name="William A. Klein">
<street>2231 Huntz Lane</street>
<city>Cambridge</city>
<state>Massachusetts(MA)</state>
<zip>02141</zip>
</shipTo>
<orderDate>2019. január 8., kedd</orderDate>
<items>
<OrderedItem>
<itemName>shovel</itemName>
<unitPrice>40.29</unitPrice>
<quantity>1</quantity>
<lineTotal>40.29</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>shovel</itemName>
<unitPrice>40.29</unitPrice>
<quantity>1</quantity>
<lineTotal>40.29</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>ballon</itemName>
<unitPrice>42.11</unitPrice>
<quantity>100</quantity>
<lineTotal>4211.00</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>ballon</itemName>
<unitPrice>42.11</unitPrice>
<quantity>100</quantity>
<lineTotal>4211.00</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>shovel</itemName>
<unitPrice>40.29</unitPrice>
<quantity>1</quantity>
<lineTotal>40.29</lineTotal>
</OrderedItem>
</items>
<subTotal>8542.87</subTotal>
<shipCost>12.5</shipCost>
<totalCost>8555.37</totalCost>
</PurchaseOrder>
<PurchaseOrder>
<shipTo name="Richard J. McCarty">
<street>2025 Heavner Court</street>
<city>Humboldt</city>
<state>Iowa(IA)</state>
<zip>50548</zip>
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 4
</shipTo>
<orderDate>2019. január 8., kedd</orderDate>
<items>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
</items>
<subTotal>36.3</subTotal>
<shipCost>12.5</shipCost>
<totalCost>48.8</totalCost>
</PurchaseOrder>
<PurchaseOrder>
<shipTo name="Marsha B. Victoria">
<street>2662 Collins Avenue</street>
<city>Westerville</city>
<state>Ohio(OH)</state>
<zip>43081</zip>
</shipTo>
<orderDate>2019. január 8., kedd</orderDate>
<items>
<OrderedItem>
<itemName>ballon</itemName>
<unitPrice>42.11</unitPrice>
<quantity>100</quantity>
<lineTotal>4211.00</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>cup</itemName>
<unitPrice>35.12</unitPrice>
<quantity>10</quantity>
<lineTotal>351.20</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
</items>
<subTotal>4671.10</subTotal>
<shipCost>12.5</shipCost>
<totalCost>4683.60</totalCost>
</PurchaseOrder>
<PurchaseOrder>
<shipTo name="Zulma S. Baker">
<street>2318 Harley Brook Lane</street>
<city>Johnstown</city>
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 5
<state>Pennsylvania(PA)</state>
<zip>15903</zip>
</shipTo>
<orderDate>2019. január 8., kedd</orderDate>
<items>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>ballon</itemName>
<unitPrice>42.11</unitPrice>
<quantity>100</quantity>
<lineTotal>4211.00</lineTotal>
</OrderedItem>
<OrderedItem>
<itemName>lamp shade</itemName>
<unitPrice>12.1</unitPrice>
<quantity>3</quantity>
<lineTotal>36.3</lineTotal>
</OrderedItem>
</items>
<subTotal>4283.60</subTotal>
<shipCost>12.5</shipCost>
<totalCost>4296.10</totalCost>
</PurchaseOrder>
</purchaseOrders>
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 6
1.2. A Possible UML Draft of the Application
1.2.1. Class Diagram
class CustomerApp
PurchaseOrder
Address
OrderedItem + customerId: int
+ id: int + city: string
+ itemName: string +shipTo + id: int Customer
+orderedItems + orderDate: string
+ lineTotal: decimal + orderedItems: OrderedItem ([]) + name: string
1 1 + + addressId: int
+ quantity: int state: string
0..* 1 + shipCost: decimal + cardNumber: string
+ unitPrice: decimal + shipTo: Address + street: string
+ id: int
+ subTotal: decimal + zip: string
+ Calculate(): void + pin: int
+ totalCost: decimal
-addresses 0..*
-orderedItems 0..* 0..*
-purchaseOrder 0..* -customers
1 1
Program
1 1
- addresses: List<Address> = new List<Addres...
- customers: List<Customer> = new List<Custom...
- orderedItems: List<OrderedItem> = new List<Ordere...
- purchaseOrder: List<PurchaseOrder> = new List<Purcha...
1 prg
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 7
1.2.2. Object Diagram
analysis Business Process Model
Object1
Obj ect2
: Address
Object3
Obj ect4
: Customer
customers : List<Customer> Contains + addressId: int = 111
1 1 5 + cardNumber: string = 774-745-7897
+ id: int = 111
+ pin: int = 5088
1 1 Object5
Obj ect6
orderedItems : OrderedItem
prg : Program orderedItems : List<> Contains + itemName: string = greeting card
1 1 1 5 + lineTotal: decimal = 10.18
+ quantity: int = 2
1 + unitPrice: decimal = 5.09
Object9
Obj ect10
orderedItems : OrderedItem
3
Contains
1
Object7
Obj ect8
: PurchaseOrder
shipTo : Address
+ customerId: int = 111
+ id: int = 111 + city: string = Cambridge
purchaseOrder : List<PurchaseOrder> Contains + orderDate: string = 2019. január 28... + id: int = 111
1 + name: string = William A. Klein
1 3 + orderedItems: OrderedItem[]
1 1 +
+ shipCost: decimal = 12.5 state: string = Massachusetts(MA)
+ shipTo: Address + street: string = 2231 Huntz Lane
+ subTotal: decimal = 423.80 + zip: string = 02141
+ totalCost: decimal = 436.30
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 8
1.4. The Solution of the Task in Detail
The identifier, the address reference, bank card number and its pin code were stored in the Customer class.
In this example, the instances of the PurchaseOrder class were serialized, then deserialized, and during these
processes, the identifier and the customer reference were stored, which using the [XmlIgnore] attribute
during serialization were not displayed. The shipping address, the date of the order, the ordered items,
the total price with and without shipping costs and the cost of shipping were stored. In case of the
ordered items, with the [XmlArray(”items”)], the names of the OrderedItems XML elements were changed
to items.
The Address class of the order was serialized as well, as regarding the type of the shipTo field of the
PurchaseOrder, it was Address and stored the identifier, which was not displayed during serialization,
moreover, the name belonging to the address, with the [XmlAttribute], as an attribute was stored in the
XML file. The street, city, state/county and the zip code were also stored, where with the
[XmlElement(IsNullable = false)], the IsNullable property set to false made sure if the value of any of
these fields was null, it did not appear in the XML file.
The OrderedItems class belonging to the order was serialized as well, as regarding the type of the OrderedItem
field of the PurchseOrder class, it was OrderedItem object block and it stored the name of the item, its unit
price, quantity and the costs of the items (unit price * quantity), which was calculated by the Calculate
function.
The Program class contains the methods implementing serialization and deserialization as well as the
created four generic lists with the following types (with previously implemented classes): Address, Customer,
OrderedItem and PurchaseOrder. For faster development, the lists were uploaded with values at creation
time. An instance of the Program class was created as well in the Main function. The createPO method,
through the instance of the Program class, reached the elements of the OrderItem typed list and the Calculate
method implemented the calculation of the costs. After this, with a LINQ query, the addresses of the
persons were quired, which were identical with the shipping addresses. The final state of the objects of
the PurchaseOrder was created. Iterating the set result of the LINQ query, the shipTo field of the given
index object of the purchaseOrder took the Address type value from the result set. The quantity of the
purchased items was created by random number generalization between 1 and 5, then depending on it, a
block of the items was created, which was uploaded with the elements of the previously uploaded
OrderedItem typed list, applying random index generalization. As the last step of the iteration, the
calculation of the expenses was implemented. Applying an instance of the XmlSerializer class created at
the beginning of the method, the XML serialization was implemented. Deserialization was implemented
by the readPO method, therefore, creating an XmlSerializer typed instance was necessary, where the object
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 9
type to be deserialized was passed for the constructor and to handle possible unknown elements and
attributes, two events were written. To read the adequate XML file, a FileStream and the objects to be
deserialized (List<PurchaseOrder>) were created. The restoring of the states of the objects in the XML file
was implemented by the Deserialize method, then the object states were displayed on the console.
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 10
1.5. The Solution of the Task
1.5.1. Customer.cs
namespace CustomerApp
{
class Customer
{
public int id;
public int addressId;
public string cardNumber;
public int pin;
}
}
1.5.2. Address.cs
namespace CustomerApp
{
public class Address
{
[XmlIgnore]
public int id;
[XmlAttribute]
public string name;
[XmlElement(IsNullable = false)]
public string street;
[XmlElement(IsNullable = false)]
public string city;
[XmlElement(IsNullable = false)]
public string state;
[XmlElement(IsNullable = false)]
public string zip;
}
}
1.5.3. OrderedItem.cs
namespace CustomerApp
{
public class OrderedItem
{
public string itemName;
public decimal unitPrice;
public int quantity;
public decimal lineTotal;
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 11
public int customerId;
public Address shipTo;
public string orderDate;
[XmlArray("items")]
public OrderedItem[] orderedItems;
public decimal subTotal;
public decimal shipCost;
public decimal totalCost;
}
}
1.5.5. Program.cs
namespace CustomerApp
{
public class Program
{
List<Address> addresses = new List<Address>()
{
new Address { id = 111, name = "William A. Klein", street = "2231 Huntz Lane",
city = "Cambridge", state = "Massachusetts(MA)", zip = "02141" },
new Address { id = 112, name = "Zulma S. Baker", street = "2318 Harley Brook
Lane", city = "Johnstown", state = "Pennsylvania(PA)", zip = "15903" },
new Address { id = 113, name = "Richard J. McCarty", street = "2025 Heavner
Court", city = "Humboldt", state = "Iowa(IA)", zip = "50548" },
new Address { id = 114, name = "Marsha B. Victoria", street = "2662 Collins
Avenue", city = "Westerville", state = "Ohio(OH)", zip = "43081" },
new Address { id = 115, name = "Joseph J. Barrett", street = "3207 Rockford
Road", city = "Bedford", state = "Massachusetts(MA)", zip = "01730" },
};
IEnumerable<Address> addressQuery =
from po in prg.purchaseOrder
join cust in prg.customers on po.customerId equals cust.id
join addr in prg.addresses on cust.addressId equals addr.id
select addr;
int i = 0;
Random rnd = new Random();
foreach (Address address in addressQuery)
{
prg.purchaseOrder[i].shipTo = address;
int itemsAmount = rnd.Next(1, 5 + 1);
prg.purchaseOrder[i].shipCost = (decimal)12.50;
prg.purchaseOrder[i].totalCost = prg.purchaseOrder[i].subTotal +
prg.purchaseOrder[i].shipCost;
i++;
}
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 14
2. Practice
2.1. StaffApp: “Staff”
2.1.1. Task Description
Design and create an application, which stores staff information in an XML file (staff.xml) with LINQ to
XML technology. Register the following data about the staff: identifier, department {Marketing, Sales,
Accounts}, name, grade, salary, bonus, home and work phone number and address. Not everybody is
entitled to getting the bonus. In case of the address, store the type {Permanent, Postal}, country, city,
zip code, state/county, street and house number. The identifier and the department should be displayed
on the second level as attributes, while the phone number and the address on the third level. The XML
file should contain the following content:
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 15
<State>Massachusetts(MA)</State>
<Zip>2141</Zip>
<Country>USA</Country>
</Address>
</Member>
<Member Id="27" Dept="Accounts">
<Name>Fred L Slowik</Name>
<Grade>4</Grade>
<Salary>28000</Salary>
<Phone Type="Home">925-469-7343</Phone>
<Phone Type="Work">818-421-8113</Phone>
<Address Type="Permanent">
<Street>4860 Canis Heights Drive</Street>
<City>Los Angeles</City>
<State>California(CA)</State>
<Zip>90071</Zip>
<Country>USA</Country>
</Address>
</Member>
<Member Id="28" Dept="Marketing">
<Name>John E Anderson</Name>
<Grade>4</Grade>
<Salary>27000</Salary>
<Phone Type="Home">305-300-4999</Phone>
<Phone Type="Work">561-754-9754</Phone>
<Address Type="Postal">
<Street>881 Fincham Road</Street>
<City>Palm Springs</City>
<State>California(CA)</State>
<Zip>92262</Zip>
<Country>USA</Country>
</Address>
</Member>
<Member Id="31" Dept="Marketing">
<Name>Anthony J Wilkerson</Name>
<Grade>6</Grade>
<Salary>18000</Salary>
<Phone Type="Home">401-770-0840</Phone>
<Phone Type="Work">401-309-1510</Phone>
<Address Type="Permanent">
<Street>3020 Oakdale Avenue</Street>
<City>Avon Park</City>
<State>Florida(FL)</State>
<Zip>33825</Zip>
<Country>USA</Country>
</Address>
</Member>
</Staff>
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 16