Programming 1: Lab 11 v1.0

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

PROGRAMMING 1

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

Creating XML trees in C# - LINQ to XML


https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/creating-xml-trees-linq-to-xml-2

IEnumerable <T> Interface


https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=netframework-4.7.2

Prepared by: Dr. Jozsef Katona, Ph.D.


e-mail: katonaj@uniduna.hu
The material of the lab can be downloaded from:
https://dufoffice365-my.sharepoint.com/:f:/g/personal/katonaj_uniduna_hu/EpFn7aKkqs9IhTSK6aC5mj0B82is3xeHCee9nbkKV3U1zw?e=zdoiXl

The example programs can be downloaded from:


https://dufoffice365-my.sharepoint.com/:f:/g/personal/katonaj_uniduna_hu/EgNyiYqpcKVPn7WPQi7dfhcBn5P3TAecQuWmssl5wlclBw?e=jK9GsE
TABLE OF CONTENTS
1. CustomerApp: “Customer” ......................................................................................................................... 3

1.1. Task Description .................................................................................................................................... 3

1.2. A Possible UML Draft of the Application ......................................................................................... 7

1.2.1. Class Diagram................................................................................................................................. 7

1.2.2. Object Diagram.............................................................................................................................. 8

1.4. The Solution of the Task in Detail ...................................................................................................... 9

1.5. The Solution of the Task .................................................................................................................... 11

1.5.1. Customer.cs .................................................................................................................................. 11

1.5.2. Address.cs ..................................................................................................................................... 11

1.5.3. OrderedItem.cs ............................................................................................................................ 11

1.5.4. PurchaseOrder.cs ......................................................................................................................... 11

1.5.5. Program.cs .................................................................................................................................... 12

2. Practice .......................................................................................................................................................... 15

2.1. StaffApp: “Staff” .................................................................................................................................. 15

2.1.1. Task Description .......................................................................................................................... 15


1. CustomerApp: “Customer”
1.1. Task Description
Create an application that serializes ordering information in an XML file (purchaseOrder.xml), then from
this file, using deserialization, displays information on the console. Register the following data about the
customer: identifier, address reference, bank card number and its pin code. In case of the address, the
following data should be stored: identifier, name, street, city, state/county and zip code. During
serialization, the name should be displayed as an attribute, but the identifier should not. Make sure that
if the street, city, state/county or zip code is null, they should not be put in the XML file. Regarding the
ordered items, store the name of the item, unit price, quantity, the multiplication of the ordered quantity
and the unit price, which should be calculated with a function. Store the identifier of the order, the
reference of the customer, the address of shipping, which is the address of the customer as well,
moreover, register the date of the order, the ordered items (this element should be displayed as Items in
the output XML file), the price of the order without shipping price, the price of shipping and the full
price that includes the shipping costs as well. The identifier and the reference should not appear in the
XML. A customer can purchase more items. The quantity of the purchased items should be a random
number between 1 and 5 per order, while the item should be a random element selected from a generic
list. During instantiation, put the objects in generic lists and upload with data in the place of creation.
Create at least five instances in case of each class. After that, create a createPO and a readPO method, where
the task of the first function is the XML serialization, while that of the second one is the implementation
of the XML deserialization. After deserialization, the XML file should have the following structure:

<?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...

- createPO(prg: Program, filename: string): void


- Main(args: string[]): void
# readAddress(a: Address, label: string): void
- readPo(prg: Program, filename: string): void 1
# serializer_UnknownAttribute(sender: object, e: XmlAttributeEventArgs): void
# serializer_UnknownNode(sender: object, e: XmlNodeEventArgs): void

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

+ city: string = Cambridge


addresses : List<Address> Contains + id: int = 111
1 1 5 + name: string = William A. Klein
+ state: string = Massachusetts(MA)
+ street: string = 2231 Huntz Lane
+ zip: string = 02141

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

+ itemName: string = lamp shade


+ lineTotal: decimal = 36.3
+ quantity: int = 3
+ unitPrice: decimal = 12.1

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;

public void Calculate()


{
lineTotal = unitPrice * quantity;
}
}
}
1.5.4. PurchaseOrder.cs
namespace CustomerApp
{
public class PurchaseOrder
{
[XmlIgnore]
public int id;
[XmlIgnore]

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" },
};

List<Customer> customers = new List<Customer>()


{
new Customer { id=111, addressId=111, cardNumber="774-745-7897", pin=5088},
new Customer { id=112, addressId=112, cardNumber="803-274-1684", pin=8037},
new Customer { id=113, addressId=113, cardNumber="313-967-1576", pin=3135},
new Customer { id=114, addressId=114, cardNumber="607-745-4425", pin=3154},
new Customer { id=115, addressId=115, cardNumber="954-385-7283", pin=4077},
};

List<OrderedItem> orderedItems = new List<OrderedItem>()


{
new OrderedItem { itemName = "greeting card", unitPrice = (decimal)5.09,
quantity = 2 },
new OrderedItem { itemName = "lamp shade", unitPrice = (decimal)12.10, quantity
= 3 },
new OrderedItem { itemName = "cup", unitPrice = (decimal)35.12, quantity = 10 },
new OrderedItem { itemName = "ballon", unitPrice = (decimal)42.11, quantity =
100 },
new OrderedItem { itemName = "shovel", unitPrice = (decimal)40.29, quantity = 1
},
};

List<PurchaseOrder> purchaseOrder = new List<PurchaseOrder>()


{
new PurchaseOrder { id=111, customerId = 111, orderDate =
DateTime.Now.ToLongDateString() },
new PurchaseOrder { id=112, customerId = 111, orderDate =
DateTime.Now.ToLongDateString() },
new PurchaseOrder { id=113, customerId = 113, orderDate =
DateTime.Now.ToLongDateString() },
new PurchaseOrder { id=114, customerId = 114, orderDate =
DateTime.Now.ToLongDateString() },
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 12
new PurchaseOrder { id=115, customerId = 112, orderDate =
DateTime.Now.ToLongDateString() },
};

static void Main(string[] args)


{
Program prg = new Program();
prg.createPO(prg, "purchaseOrder.xml");
prg.readPo("purchaseOrder.xml");
}

private void createPO(Program prg, string filename)


{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<PurchaseOrder>), new
XmlRootAttribute("purchaseOrders"));

foreach (OrderedItem item in prg.orderedItems)


item.Calculate();

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);

OrderedItem[] items = new OrderedItem[itemsAmount];


for (int j = 0; j < itemsAmount; j++)
items[j] = orderedItems.ElementAt(rnd.Next(1, 4+1));
prg.purchaseOrder[i].orderedItems = items;

decimal subTotal = new decimal();


foreach (OrderedItem oi in items)
subTotal += oi.lineTotal;
prg.purchaseOrder[i].subTotal = subTotal;

prg.purchaseOrder[i].shipCost = (decimal)12.50;
prg.purchaseOrder[i].totalCost = prg.purchaseOrder[i].subTotal +
prg.purchaseOrder[i].shipCost;
i++;
}

using (FileStream fs = new FileStream(filename, FileMode.Create))


xmlSerializer.Serialize(fs, purchaseOrder);
}

private void readPo(string filename)


{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<PurchaseOrder>), new
XmlRootAttribute("purchaseOrders"));
xmlSerializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
xmlSerializer.UnknownAttribute += new
XmlAttributeEventHandler(serializer_UnknownAttribute);

FileStream fs = new FileStream(filename, FileMode.Open);


List<PurchaseOrder> pos;
Programozás 1 - DUEN-ISF-213-HU
Készítette: Dr. Katona József, PhD (katonaj@uniduna.hu) 13
pos = (List<PurchaseOrder>)xmlSerializer.Deserialize(fs);

foreach (PurchaseOrder po in pos)


{
Console.WriteLine("OrderDate: " + po.orderDate);

Address address = po.shipTo;


readAddress(address, "Ship To:");
OrderedItem[] items = po.orderedItems;
Console.WriteLine("Items to be shipped:");
foreach (OrderedItem item in items)
{
Console.WriteLine("\t" +
item.itemName + "\t" +
item.unitPrice + "\t" +
item.quantity + "\t" +
item.lineTotal);
}
Console.WriteLine(
"\n\t\t\t\t\t Subtotal\t" + po.subTotal +
"\n\t\t\t\t\t Shipping\t" + po.shipCost +
"\n\t\t\t\t\t Total\t\t" + po.totalCost
);
}
}

protected void readAddress(Address a, string label)


{
Console.WriteLine(label);
Console.Write("\t" +
a.name + "\n\t" +
a.street + "\n\t" +
a.city + "\t" +
a.state + "\n\t" +
a.zip + "\n");
}

protected void serializer_UnknownNode(object sender, XmlNodeEventArgs e)


{
Console.WriteLine("Unknown Node:" + e.Name + "\t" + e.Text);
}

protected void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)


{
System.Xml.XmlAttribute attr = e.Attr;
Console.WriteLine("Unknown attribute " + attr.Name + "='" + attr.Value + "'");
}
}
}

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:

<?xml version="1.0" encoding="utf-8"?>


<Staff>
<Member Id="21" Dept="Marketing">
<Name>Mark B. Harbour</Name>
<Grade>6</Grade>
<Salary>20000</Salary>
<Phone Type="Home">973-475-1512</Phone>
<Phone Type="Work">937-823-2348</Phone>
<Address Type="Postal">
<Street>3971 Ingram Street</Street>
<City>Dayton</City>
<State>Ohio(OH)</State>
<Zip>45402</Zip>
<Country>USA</Country>
</Address>
</Member>
<Member Id="23" Dept="Sales">
<Name>Charles M Stewart</Name>
<Grade>6</Grade>
<Salary>22000</Salary>
<Bonus>0.03</Bonus>
<Phone Type="Home">740-803-7350</Phone>
<Phone Type="Work">740-272-9206</Phone>
<Address Type="Postal">
<Street>3483</Street>
<City>Delaware</City>
<State>Ohio(OH)</State>
<Zip>43015</Zip>
<Country>USA</Country>
</Address>
</Member>
<Member Id="24" Dept="Sales">
<Name>Liz Elbow</Name>
<Grade>5</Grade>
<Salary>22500</Salary>
<Bonus>0.02</Bonus>
<Phone Type="Home">863-474-6774</Phone>
<Phone Type="Work">305-201-9879</Phone>
<Address Type="Permanent">
<Street>1732 Single Street</Street>
<City>Cambridge</City>

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

You might also like