LINQ : how to read the XML file using an XmlReader

Reading the XML file of orders using an XmlReader
String nsUri = "http://schemas.devleap.com/Orders";
XmlReader xmlOrders = XmlReader.Create( "Orders.xml" );
List<Order> orders = new List<Order>();
Order order = null;
while (xmlOrders.Read()) {
switch (xmlOrders.NodeType) {
case XmlNodeType.Element:
if ((xmlOrders.Name == "order") &&
(xmlOrders.NamespaceURI == nsUri)) {
order = new Order();
order.CustomerID = xmlOrders.GetAttribute( "idCustomer" );
order.Product = new Product();
order.Product.IdProduct =
Int32.Parse( xmlOrders.GetAttribute( "idProduct" ) );
order.Product.Price =
Decimal.Parse( xmlOrders.GetAttribute( "price" ) );
10 Introducing Microsoft LINQ
order.Quantity =
Int32.Parse( xmlOrders.GetAttribute( "quantity" ) );
orders.Add( order );
}
break;
}
}

No comments:

Post a Comment