Advertisement
Guest User

supplier2bh

a guest
Jun 30th, 2016
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Xml;
  14. using System.Xml.Linq;
  15.  
  16. namespace Supplier2
  17. {
  18.     public partial class ReadOrderContent : Form
  19.     {
  20.         public ReadOrderContent()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         public XmlDocument loadedOrderXML;
  26.         public Stream currentStream;
  27.         public ArrayList currentOrderList;
  28.         public ArrayList currentOrderDetailList;
  29.  
  30.         private void label1_Click(object sender, EventArgs e)
  31.         {
  32.  
  33.         }
  34.  
  35.         private void label2_Click(object sender, EventArgs e)
  36.         {
  37.  
  38.         }
  39.  
  40.         public XmlDocument browseXML()
  41.         {
  42.             OpenFileDialog ofd = new OpenFileDialog();
  43.             ofd.InitialDirectory = "ftp://Supplier2:Supplier2@175.156.148.165/OrderXML/";
  44.             ofd.Filter = "XML Files|*.xml";
  45.             ofd.FilterIndex = 1;
  46.             ofd.RestoreDirectory = true;
  47.             ofd.ShowDialog();
  48.  
  49.             using (var myStream = ofd.OpenFile())
  50.             {
  51.                 try
  52.                 {
  53.                     //  Successfully return the XML
  54.                     XmlDocument parsedMyStream = new XmlDocument();
  55.                     parsedMyStream.Load(myStream); //parse stream as xml document
  56.                
  57.                     //previewing the XML
  58.                     string xml = parsedMyStream.OuterXml;
  59.                     XDocument xDocument = XDocument.Parse(xml);
  60.                     PreviewBox.Text = xDocument.ToString();
  61.                     //load xml to currently loaded xml container
  62.                     loadedOrderXML = parsedMyStream;
  63.                     //show name of the file loaded
  64.                     loadedXMLNAME.Text = ofd.SafeFileName;
  65.                     return parsedMyStream;
  66.                 }
  67.                 catch (XmlException ex)
  68.                 {
  69.                     DialogResult dr = MessageBox.Show("The XML could not be read. " + ex);
  70.                     XmlDocument blank = new XmlDocument();
  71.                     return blank;
  72.                 }
  73.             }
  74.         }
  75.  
  76.         private void button1_Click(object sender, EventArgs e) //browse
  77.         {
  78.             browseXML();
  79.         }
  80.         public void generateInvoice()
  81.         {
  82.  
  83.             XmlDocument orderDoc = new XmlDocument();
  84.             string docPath = "../../Invoice.xml";
  85.  
  86.             // Write down the XML declaration
  87.             XmlDeclaration xmlDeclaration = orderDoc.CreateXmlDeclaration("1.0", "utf-8", null);
  88.  
  89.             // Create the root element
  90.             //start xml for invoice node
  91.             XmlElement invoice = orderDoc.CreateElement("Invoices");
  92.             orderDoc.InsertBefore(xmlDeclaration, orderDoc.DocumentElement);
  93.             orderDoc.AppendChild(invoice);
  94.  
  95.             //parent node and attribtute
  96.             XmlElement parentInvoice = orderDoc.CreateElement("Invoice");
  97.             parentInvoice.SetAttribute("ID", "I1");
  98.             orderDoc.DocumentElement.PrependChild(parentInvoice);
  99.  
  100.             //elements under Invoice node. before InvoiceItem node
  101.             XmlElement invoiceDate = orderDoc.CreateElement("InvoiceDate");
  102.             XmlElement seller = orderDoc.CreateElement("SellerID");
  103.             XmlElement buyer = orderDoc.CreateElement("BuyerID");
  104.             XmlElement orderID = orderDoc.CreateElement("OrderID");
  105.  
  106.             //populating the nodes with values
  107.  
  108.             XmlText dateText = orderDoc.CreateTextNode("21/06/2016");
  109.             XmlText sellerText = orderDoc.CreateTextNode("Supp002");
  110.             XmlText buyerText = orderDoc.CreateTextNode("WCS1810");
  111.             XmlText orderIDText = orderDoc.CreateTextNode("O3");
  112.  
  113.  
  114.             //append to parentnode Invoice
  115.             parentInvoice.AppendChild(invoiceDate);
  116.             parentInvoice.AppendChild(seller);
  117.             parentInvoice.AppendChild(buyer);
  118.             parentInvoice.AppendChild(orderID);
  119.  
  120.             //save above values    
  121.             invoiceDate.AppendChild(dateText);
  122.             seller.AppendChild(sellerText);
  123.             buyer.AppendChild(buyerText);
  124.             orderID.AppendChild(orderIDText);
  125.  
  126.             //start new parent node, withh InvoiceItem as parent node2.    
  127.             XmlElement parentInvoiceItem = orderDoc.CreateElement("InvoiceItem");
  128.             parentInvoice.AppendChild(parentInvoiceItem);
  129.             //parent node and attribtute
  130.             XmlElement parentProduct = orderDoc.CreateElement("Product");
  131.             parentProduct.SetAttribute("ID", "I1");
  132.             parentInvoiceItem.AppendChild(parentProduct);
  133.  
  134.  
  135.             //elements under product node
  136.             XmlElement productName = orderDoc.CreateElement("ProductName");
  137.             XmlElement description = orderDoc.CreateElement("Description");
  138.             XmlElement capacity = orderDoc.CreateElement("Capacity");
  139.             XmlElement quantity = orderDoc.CreateElement("Quantity");
  140.             XmlElement unitPrice = orderDoc.CreateElement("UnitPrice");
  141.  
  142.             XmlText productText = orderDoc.CreateTextNode("8GB RAM King");
  143.             XmlText descriptionText = orderDoc.CreateTextNode("8GB RAM King Brand");
  144.             XmlText capacityText = orderDoc.CreateTextNode("8GB");
  145.             XmlText quantityText = orderDoc.CreateTextNode("150");
  146.             XmlText unitPriceText = orderDoc.CreateTextNode("100");
  147.  
  148.             parentProduct.AppendChild(productName);
  149.             parentProduct.AppendChild(description);
  150.             parentProduct.AppendChild(capacity);
  151.             parentProduct.AppendChild(quantity);
  152.             parentProduct.AppendChild(unitPrice);
  153.  
  154.             productName.AppendChild(productText);
  155.             description.AppendChild(descriptionText);
  156.             capacity.AppendChild(capacityText);
  157.             quantity.AppendChild(quantityText);
  158.             unitPrice.AppendChild(unitPriceText);
  159.  
  160.             XmlElement shippingCharge = orderDoc.CreateElement("ShippingCharges");
  161.             XmlElement invoiceTotal = orderDoc.CreateElement("InvoiceTotal");
  162.  
  163.             XmlText shippingText = orderDoc.CreateTextNode("5");
  164.             XmlText invoiceText = orderDoc.CreateTextNode("205");
  165.  
  166.             parentInvoice.AppendChild(shippingCharge);
  167.             parentInvoice.AppendChild(invoiceTotal);
  168.  
  169.             shippingCharge.AppendChild(shippingText);
  170.             invoiceTotal.AppendChild(invoiceText);
  171.  
  172.  
  173.             orderDoc.Save(docPath);
  174.  
  175.  
  176.  
  177.  
  178.         }
  179.  
  180.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  181.         {
  182.  
  183.         }
  184.  
  185.         private void addToDB_Click(object sender, EventArgs e)
  186.         {
  187.             try
  188.             {
  189.                 DBManager orderInfo = new DBManager();
  190.                
  191.                 DialogResult dr = MessageBox.Show("The data has been stored to the database.");
  192.             }
  193.             catch (Exception)
  194.             {
  195.                 DialogResult dr = MessageBox.Show("Failed to order information to database.");
  196.             }
  197.         }
  198.  
  199.         private void Check2_Click(object sender, EventArgs e)
  200.         {
  201.             string result = "";
  202.             Order order = new Order();
  203.             OrderDetail orderd = new OrderDetail();
  204.             ArrayList orderList = new ArrayList();
  205.             ArrayList orderDetailList = new ArrayList();
  206.             string orderID = "";
  207.  
  208.             XmlReader reader = XmlReader.Create(new StringReader(loadedOrderXML.OuterXml));
  209.             while (reader.ReadToFollowing("Orders"))
  210.             {
  211.                 reader.MoveToFirstAttribute();
  212.                 order.OrderID = reader.Value;
  213.                 orderID = reader.Value;
  214.  
  215.                 reader.ReadToFollowing("OrderDate");
  216.                 order.OrderDate = reader.ReadElementContentAsString();
  217.  
  218.                 reader.ReadToFollowing("BuyerID");
  219.                 order.CustomerID = reader.ReadElementContentAsString();
  220.  
  221.                 reader.ReadToFollowing("Instructions");
  222.                 order.Instructions = reader.ReadElementContentAsString();
  223.  
  224.                 orderList.Add(order);
  225.                 while (reader.ReadToFollowing("OrderItem"))
  226.                 {
  227.                     OrderDetail i = new OrderDetail();
  228.                     i.OrderID = orderID;
  229.  
  230.                     reader.MoveToFirstAttribute();
  231.                     i.ItemID = reader.Value;
  232.  
  233.                     reader.ReadToFollowing("Description");
  234.                     i.Description = reader.ReadElementContentAsString();
  235.  
  236.                     reader.ReadToFollowing("Quantities");
  237.                     i.Quantity = reader.ReadElementContentAsInt();
  238.  
  239.                     reader.ReadToFollowing("ExistingUnitPrices");
  240.                     i.AskingPrice = reader.ReadElementContentAsDecimal();
  241.  
  242.                     reader.ReadToFollowing("Capacity");
  243.                     i.Capacity = reader.ReadElementContentAsString();
  244.  
  245.                     orderDetailList.Add(i);
  246.                 }
  247.             }
  248.  
  249.            
  250.             currentOrderList = orderList;
  251.             currentOrderDetailList = orderDetailList;
  252.  
  253.             //display order data into richtextbox
  254.             string display = "";
  255.             foreach (Order ii in currentOrderList)
  256.             {
  257.                 display += ii.ToString() + "\r\n";
  258.                 display += "---------------" + "\r\n";
  259.             }
  260.             orderData.Text = display;
  261.  
  262.  
  263.             string display2 = "";
  264.             foreach (OrderDetail ii in currentOrderDetailList)
  265.             {
  266.                 display2 += ii.ToString() + "\r\n";
  267.                 display2 += "---------------" + "\r\n";
  268.             }
  269.             orderDetailData.Text = display2;
  270.         }
  271.  
  272.         private void PreviewBox_TextChanged(object sender, EventArgs e)
  273.         {
  274.  
  275.         }
  276.  
  277.         private void ReadOrderContent_Load(object sender, EventArgs e)
  278.         {
  279.  
  280.         }
  281.  
  282.         private void label2_Click_1(object sender, EventArgs e)
  283.         {
  284.  
  285.         }
  286.  
  287.         private void orderData_TextChanged(object sender, EventArgs e)
  288.         {
  289.  
  290.         }
  291.  
  292.         private void orderDetail_TextChanged(object sender, EventArgs e)
  293.         {
  294.  
  295.         }
  296.  
  297.         private void orderDetailData_TextChanged(object sender, EventArgs e)
  298.         {
  299.  
  300.         }
  301.  
  302.         private void orderData_TextChanged_1(object sender, EventArgs e)
  303.         {
  304.  
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement