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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Linq;
- namespace CaptureInvoiceInformation
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- OpenXML();
- //using DOM approach to parse XML file
- // process XML data using DOM approach
- }
- //curent opened xml session
- public XmlDocument currentXML;
- //opening specified xml
- public XmlDocument OpenXML()
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.InitialDirectory = "ftp://xxx:xxx@xxx.xxx.xxx.xx";
- ofd.Filter = "XML Files|*.xml";
- ofd.FilterIndex = 1;
- ofd.RestoreDirectory = true;
- ofd.ShowDialog();
- // The stream will hold the results of opening the XML
- using (var myStream = ofd.OpenFile())
- {
- try
- {
- // Successfully return the XML
- XmlDocument parsedMyStream = new XmlDocument();
- parsedMyStream.Load(myStream);
- //previewing the XML
- string xml =parsedMyStream.OuterXml;
- XDocument xDocument = XDocument.Parse(xml);
- XMLPreview.Text = xDocument.ToString();
- currentXML = parsedMyStream;
- return parsedMyStream;
- }
- catch (XmlException ex)
- {
- MessageBox.Show("The XML could not be read. " + ex);
- return CreateEmptyXmlDocument();
- }
- }
- }
- public XmlDocument XmlDocument { get; private set; }
- private XmlDocument CreateEmptyXmlDocument()
- {
- // Return an empty XmlDocument if the open file window was closed
- XmlDocument emptyMyStream = new XmlDocument();
- return emptyMyStream;
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void XMLPreview_TextChanged(object sender, EventArgs e)
- {
- }
- private void AddDB_Click(object sender, EventArgs e)
- {
- //using DOM approach to parse XML file
- // process XML data using DOM approach
- XmlDocument invoiceXML = currentXML;
- XmlNodeList invoiceList = invoiceXML.GetElementsByTagName("Invoice");
- XmlNodeList productList = invoiceXML.GetElementsByTagName("Product");
- ArrayList invoices = new ArrayList();
- Invoice invoice = new Invoice();
- foreach (XmlNode node in invoiceList)
- {
- invoice = new Invoice();
- invoice.InvoiceID = node.Attributes[0].Value;
- invoice.InvoiceDate = node.ChildNodes[0].InnerText;
- invoice.SellerID = node.ChildNodes[1].InnerText;
- invoice.OrderID = node.ChildNodes[3].InnerText;
- decimal totalItemPrice = 0;
- foreach (XmlNode node2 in productList)
- {
- decimal qty = Convert.ToDecimal(node.ChildNodes[3].InnerText);
- decimal unitpx = Convert.ToDecimal(node.ChildNodes[4].InnerText);
- totalItemPrice += (qty * unitpx);
- }
- invoice.ItemsTotalPrice = totalItemPrice;
- invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
- invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);
- invoices.Add(invoice);
- }
- //insert into DB
- DBManager dbmgr = new DBManager();
- dbmgr.insertInvoice(invoices);
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void richTextBox2_TextChanged(object sender, EventArgs e)
- {
- }
- private void checkInvoiceItem_Click(object sender, EventArgs e)
- {
- }
- private void checkInvoice_Click(object sender, EventArgs e)
- {
- XmlDocument invoiceXML = currentXML;
- XmlNodeList invoiceList = invoiceXML.GetElementsByTagName("Invoice");
- XmlNodeList productList = invoiceXML.GetElementsByTagName("Product");
- ArrayList invoices = new ArrayList();
- Invoice invoice = new Invoice();
- foreach (XmlNode node in invoiceList)
- {
- invoice = new Invoice();
- invoice.InvoiceID = node.Attributes[0].Value;
- invoice.InvoiceDate = node.ChildNodes[0].InnerText;
- invoice.SellerID = node.ChildNodes[1].InnerText;
- invoice.OrderID = node.ChildNodes[3].InnerText;
- decimal totalItemPrice = 0;
- foreach (XmlNode node2 in productList)
- {
- decimal qty = Convert.ToDecimal(node.ChildNodes[3].InnerText);
- decimal unitpx = Convert.ToDecimal(node.ChildNodes[4].InnerText);
- totalItemPrice += (qty * unitpx);
- }
- invoice.ItemsTotalPrice = totalItemPrice;
- invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
- invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);
- invoices.Add(invoice);
- }
- StringBuilder sb = new StringBuilder();
- foreach(object obj in invoices)
- {
- sb.Append(obj);
- }
- richTextBox1.Text = sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement