private MailMergeDataTable GetTestOrderDetails(int TestOrderId) { List <TestOrderDetail> orders = new List <TestOrderDetail>(); FileStream fs = new FileStream(_hostingEnvironment.WebRootPath + @"/DocIO/TestOrderDetails.xml", FileMode.Open, FileAccess.Read); XmlReader reader = XmlReader.Create(fs); if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "TestOrderDetails") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } while (reader.LocalName != "TestOrderDetails") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "TestOrderDetail": TestOrderDetail testOrder = GetTestOrderDetail(reader); if (testOrder.OrderID == TestOrderId.ToString()) { orders.Add(testOrder); break; } break; } } else { reader.Read(); if ((reader.LocalName == "TestOrderDetails") && reader.NodeType == XmlNodeType.EndElement) { break; } } } MailMergeDataTable dataTable = new MailMergeDataTable("Order", orders); reader.Dispose(); fs.Dispose(); return(dataTable); }
private TestOrderDetail GetTestOrderDetail(XmlReader reader) { if (reader == null) { throw new Exception("reader"); } while (reader.NodeType != XmlNodeType.Element) { reader.Read(); } if (reader.LocalName != "TestOrderDetail") { throw new XmlException("Unexpected xml tag " + reader.LocalName); } reader.Read(); while (reader.NodeType == XmlNodeType.Whitespace) { reader.Read(); } TestOrderDetail testOrderDetail = new TestOrderDetail(); while (reader.LocalName != "TestOrderDetail") { if (reader.NodeType == XmlNodeType.Element) { switch (reader.LocalName) { case "OrderID": testOrderDetail.OrderID = reader.ReadElementContentAsString(); break; case "ProductID": testOrderDetail.ProductID = reader.ReadElementContentAsString(); break; case "ProductName": testOrderDetail.ProductName = reader.ReadElementContentAsString(); break; case "UnitPrice": testOrderDetail.UnitPrice = reader.ReadElementContentAsString(); break; case "Quantity": testOrderDetail.Quantity = reader.ReadElementContentAsString(); break; case "Discount": testOrderDetail.Discount = reader.ReadElementContentAsString(); break; case "ExtendedPrice": testOrderDetail.ExtendedPrice = reader.ReadElementContentAsString(); break; default: reader.Skip(); break; } } else { reader.Read(); if ((reader.LocalName == "TestOrderDetail") && reader.NodeType == XmlNodeType.EndElement) { break; } } } return(testOrderDetail); }