// Mapping an business object for an order to an XElement for an invoice public static XElement OoOrder2XmlInvoice(OO.Order o) { return(new XElement(ins + "Invoice", new XElement(ins + "Name", o.Cust.Name), new XElement(ins + "Street", o.Cust.Addr.Street), new XElement(ins + "City", o.Cust.Addr.City), new XElement(ins + "Zip", o.Cust.Addr.Zip), new XElement(ins + "State", o.Cust.Addr.State), from i in o.Items select new XElement(ins + "Position", new XElement(ins + "ProdId", i.Prod.Id), new XElement(ins + "Price", i.Price), new XElement(ins + "Quantity", i.Quantity)), new XElement(ins + "Total", o.Total()))); }
public static void RunAll() { // Setting up test data var bo = new Order { Cust = new Customer { Id = "0815", Name = "Fred Mueller", Addr = new Address { Street = "1234, BelRed Road", City = "Redmond", Zip = "98052", State = "WA" }}, Items = new List<Item> { new Item { Prod = new Product { Id = "1234", Price = 44, Quantity = 111 }, Price = 37.0, Quantity = 2 }, new Item { Prod = new Product { Id = "5678", Price = 3, Quantity = 222 }, Price = 1.5, Quantity = 3 }}}; var xo1 = XElement.Load("../../Data/Order.xml"); var xo2 = (www.vertical.com.Order.Order)xo1; // LINQ to XML var i1 = LinqToXml.Mapping.OoOrder2XmlInvoice(bo); i1.CompareWithBaseline("Invoice"); var o1 = LinqToXml.Mapping.XmlOrder2OoOrder(xo1); // LINQ to XSD var i2 = LinqToXsd.Mapping.OoOrder2XmlInvoice(bo); i2.CompareWithBaseline("Invoice"); var o2 = LinqToXsd.Mapping.XmlOrder2OoOrder(xo2); // VB9 XML integration var i3 = XmlIntegration.Mapping.OoOrder2XmlInvoice(bo); // We do not run comparison because of a Beta1 issue. // i3.CompareWithBaseline("Invoice"); var o3 = XmlIntegration.Mapping.XmlOrder2OoOrder(xo1); // DOM var i4 = DOM.Mapping.OoOrder2XmlInvoice(bo); i4.CompareWithBaseline("Invoice"); // Data Contract var i5 = WCF.Mapping.OoOrder2XmlInvoice(bo); i5.Save("../../DataContract/Invoice.xml"); }
// Mapping a business object for an order to an invoice object of a schema-derived class public static Xml.Invoice OoOrder2XmlInvoice(OO.Order o) { return(new Xml.Invoice { Name = o.Cust.Name, Street = o.Cust.Addr.Street, City = o.Cust.Addr.City, Zip = o.Cust.Addr.Zip, State = o.Cust.Addr.State, Position = (from i in o.Items select new Xml.Position { ProdId = i.Prod.Id, Price = i.Price, Quantity = i.Quantity }).ToList(), Total = o.Total() }); }
// Mapping an business object for an order to an XmlDocument for an invoice public static XmlDocument OoOrder2XmlInvoice(OO.Order o) { string ns = "http://www.vertical.com/Invoice"; var doc = new XmlDocument(); var inv = doc.CreateElement("Invoice", ns); doc.AppendChild(inv); var name = doc.CreateElement("Name", ns); inv.AppendChild(name); name.AppendChild(doc.CreateTextNode(o.Cust.Name)); // Ditto for street, city, zip, state var street = doc.CreateElement("Street", ns); inv.AppendChild(street); street.AppendChild(doc.CreateTextNode(o.Cust.Addr.Street)); var city = doc.CreateElement("City", ns); inv.AppendChild(city); city.AppendChild(doc.CreateTextNode(o.Cust.Addr.City)); var zip = doc.CreateElement("Zip", ns); inv.AppendChild(zip); zip.AppendChild(doc.CreateTextNode(o.Cust.Addr.Zip)); var state = doc.CreateElement("State", ns); inv.AppendChild(state); state.AppendChild(doc.CreateTextNode(o.Cust.Addr.State)); foreach (var i in o.Items) { var item = doc.CreateElement("Position", ns); inv.AppendChild(item); var prodid = doc.CreateElement("ProdId", ns); item.AppendChild(prodid); prodid.AppendChild(doc.CreateTextNode(i.Prod.Id)); // Ditto for price, quantity var price = doc.CreateElement("Price", ns); item.AppendChild(price); price.AppendChild(doc.CreateTextNode(XmlConvert.ToString(i.Price))); var quantity = doc.CreateElement("Quantity", ns); item.AppendChild(quantity); quantity.AppendChild(doc.CreateTextNode(i.Quantity.ToString())); } var total = doc.CreateElement("Total", ns); inv.AppendChild(total); total.AppendChild(doc.CreateTextNode(o.Total().ToString())); return(doc); }