Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Linq;
- namespace Supplier2
- {
- public partial class ReadOrderContent : Form
- {
- public ReadOrderContent()
- {
- InitializeComponent();
- }
- public XmlDocument loadedOrderXML;
- public Stream currentStream;
- public ArrayList currentOrderList;
- public ArrayList currentOrderDetailList;
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- public XmlDocument browseXML()
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.InitialDirectory = "ftp://Supplier2:Supplier2@175.156.148.165/OrderXML/";
- ofd.Filter = "XML Files|*.xml";
- ofd.FilterIndex = 1;
- ofd.RestoreDirectory = true;
- ofd.ShowDialog();
- using (var myStream = ofd.OpenFile())
- {
- try
- {
- // Successfully return the XML
- XmlDocument parsedMyStream = new XmlDocument();
- parsedMyStream.Load(myStream); //parse stream as xml document
- //previewing the XML
- string xml = parsedMyStream.OuterXml;
- XDocument xDocument = XDocument.Parse(xml);
- PreviewBox.Text = xDocument.ToString();
- //load xml to currently loaded xml container
- loadedOrderXML = parsedMyStream;
- //show name of the file loaded
- loadedXMLNAME.Text = ofd.SafeFileName;
- return parsedMyStream;
- }
- catch (XmlException ex)
- {
- DialogResult dr = MessageBox.Show("The XML could not be read. " + ex);
- XmlDocument blank = new XmlDocument();
- return blank;
- }
- }
- }
- private void button1_Click(object sender, EventArgs e) //browse
- {
- browseXML();
- }
- public void generateInvoice()
- {
- XmlDocument orderDoc = new XmlDocument();
- string docPath = "../../Invoice.xml";
- // Write down the XML declaration
- XmlDeclaration xmlDeclaration = orderDoc.CreateXmlDeclaration("1.0", "utf-8", null);
- // Create the root element
- //start xml for invoice node
- XmlElement invoice = orderDoc.CreateElement("Invoices");
- orderDoc.InsertBefore(xmlDeclaration, orderDoc.DocumentElement);
- orderDoc.AppendChild(invoice);
- //parent node and attribtute
- XmlElement parentInvoice = orderDoc.CreateElement("Invoice");
- parentInvoice.SetAttribute("ID", "I1");
- orderDoc.DocumentElement.PrependChild(parentInvoice);
- //elements under Invoice node. before InvoiceItem node
- XmlElement invoiceDate = orderDoc.CreateElement("InvoiceDate");
- XmlElement seller = orderDoc.CreateElement("SellerID");
- XmlElement buyer = orderDoc.CreateElement("BuyerID");
- XmlElement orderID = orderDoc.CreateElement("OrderID");
- //populating the nodes with values
- XmlText dateText = orderDoc.CreateTextNode("21/06/2016");
- XmlText sellerText = orderDoc.CreateTextNode("Supp002");
- XmlText buyerText = orderDoc.CreateTextNode("WCS1810");
- XmlText orderIDText = orderDoc.CreateTextNode("O3");
- //append to parentnode Invoice
- parentInvoice.AppendChild(invoiceDate);
- parentInvoice.AppendChild(seller);
- parentInvoice.AppendChild(buyer);
- parentInvoice.AppendChild(orderID);
- //save above values
- invoiceDate.AppendChild(dateText);
- seller.AppendChild(sellerText);
- buyer.AppendChild(buyerText);
- orderID.AppendChild(orderIDText);
- //start new parent node, withh InvoiceItem as parent node2.
- XmlElement parentInvoiceItem = orderDoc.CreateElement("InvoiceItem");
- parentInvoice.AppendChild(parentInvoiceItem);
- //parent node and attribtute
- XmlElement parentProduct = orderDoc.CreateElement("Product");
- parentProduct.SetAttribute("ID", "I1");
- parentInvoiceItem.AppendChild(parentProduct);
- //elements under product node
- XmlElement productName = orderDoc.CreateElement("ProductName");
- XmlElement description = orderDoc.CreateElement("Description");
- XmlElement capacity = orderDoc.CreateElement("Capacity");
- XmlElement quantity = orderDoc.CreateElement("Quantity");
- XmlElement unitPrice = orderDoc.CreateElement("UnitPrice");
- XmlText productText = orderDoc.CreateTextNode("8GB RAM King");
- XmlText descriptionText = orderDoc.CreateTextNode("8GB RAM King Brand");
- XmlText capacityText = orderDoc.CreateTextNode("8GB");
- XmlText quantityText = orderDoc.CreateTextNode("150");
- XmlText unitPriceText = orderDoc.CreateTextNode("100");
- parentProduct.AppendChild(productName);
- parentProduct.AppendChild(description);
- parentProduct.AppendChild(capacity);
- parentProduct.AppendChild(quantity);
- parentProduct.AppendChild(unitPrice);
- productName.AppendChild(productText);
- description.AppendChild(descriptionText);
- capacity.AppendChild(capacityText);
- quantity.AppendChild(quantityText);
- unitPrice.AppendChild(unitPriceText);
- XmlElement shippingCharge = orderDoc.CreateElement("ShippingCharges");
- XmlElement invoiceTotal = orderDoc.CreateElement("InvoiceTotal");
- XmlText shippingText = orderDoc.CreateTextNode("5");
- XmlText invoiceText = orderDoc.CreateTextNode("205");
- parentInvoice.AppendChild(shippingCharge);
- parentInvoice.AppendChild(invoiceTotal);
- shippingCharge.AppendChild(shippingText);
- invoiceTotal.AppendChild(invoiceText);
- orderDoc.Save(docPath);
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void addToDB_Click(object sender, EventArgs e)
- {
- try
- {
- DBManager orderInfo = new DBManager();
- DialogResult dr = MessageBox.Show("The data has been stored to the database.");
- }
- catch (Exception)
- {
- DialogResult dr = MessageBox.Show("Failed to order information to database.");
- }
- }
- private void Check2_Click(object sender, EventArgs e)
- {
- string result = "";
- Order order = new Order();
- OrderDetail orderd = new OrderDetail();
- ArrayList orderList = new ArrayList();
- ArrayList orderDetailList = new ArrayList();
- string orderID = "";
- XmlReader reader = XmlReader.Create(new StringReader(loadedOrderXML.OuterXml));
- while (reader.ReadToFollowing("Orders"))
- {
- reader.MoveToFirstAttribute();
- order.OrderID = reader.Value;
- orderID = reader.Value;
- reader.ReadToFollowing("OrderDate");
- order.OrderDate = reader.ReadElementContentAsString();
- reader.ReadToFollowing("BuyerID");
- order.CustomerID = reader.ReadElementContentAsString();
- reader.ReadToFollowing("Instructions");
- order.Instructions = reader.ReadElementContentAsString();
- orderList.Add(order);
- while (reader.ReadToFollowing("OrderItem"))
- {
- OrderDetail i = new OrderDetail();
- i.OrderID = orderID;
- reader.MoveToFirstAttribute();
- i.ItemID = reader.Value;
- reader.ReadToFollowing("Description");
- i.Description = reader.ReadElementContentAsString();
- reader.ReadToFollowing("Quantities");
- i.Quantity = reader.ReadElementContentAsInt();
- reader.ReadToFollowing("ExistingUnitPrices");
- i.AskingPrice = reader.ReadElementContentAsDecimal();
- reader.ReadToFollowing("Capacity");
- i.Capacity = reader.ReadElementContentAsString();
- orderDetailList.Add(i);
- }
- }
- currentOrderList = orderList;
- currentOrderDetailList = orderDetailList;
- //display order data into richtextbox
- string display = "";
- foreach (Order ii in currentOrderList)
- {
- display += ii.ToString() + "\r\n";
- display += "---------------" + "\r\n";
- }
- orderData.Text = display;
- string display2 = "";
- foreach (OrderDetail ii in currentOrderDetailList)
- {
- display2 += ii.ToString() + "\r\n";
- display2 += "---------------" + "\r\n";
- }
- orderDetailData.Text = display2;
- }
- private void PreviewBox_TextChanged(object sender, EventArgs e)
- {
- }
- private void ReadOrderContent_Load(object sender, EventArgs e)
- {
- }
- private void label2_Click_1(object sender, EventArgs e)
- {
- }
- private void orderData_TextChanged(object sender, EventArgs e)
- {
- }
- private void orderDetail_TextChanged(object sender, EventArgs e)
- {
- }
- private void orderDetailData_TextChanged(object sender, EventArgs e)
- {
- }
- private void orderData_TextChanged_1(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement