Advertisement
tezuka777

XML DOM

Jun 27th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.22 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.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13.  
  14. namespace CaptureInvoiceInformation
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             OpenXML();
  26.             //using DOM approach to parse XML file
  27.             // process XML data using DOM approach            
  28.         }
  29.        
  30.         //curent opened xml session
  31.         public XmlDocument currentXML;
  32.         //opening specified xml
  33.         public XmlDocument OpenXML()
  34.         {
  35.             OpenFileDialog ofd = new OpenFileDialog();
  36.             ofd.InitialDirectory = "ftp://xxx:xxx@xxx.xxx.xxx.xx";
  37.             ofd.Filter = "XML Files|*.xml";
  38.             ofd.FilterIndex = 1;
  39.             ofd.RestoreDirectory = true;
  40.             ofd.ShowDialog();
  41.             //  The stream will hold the results of opening the XML  
  42.             using (var myStream = ofd.OpenFile())
  43.             {
  44.                 try
  45.                 {
  46.                     //  Successfully return the XML
  47.                     XmlDocument parsedMyStream = new XmlDocument();
  48.                     parsedMyStream.Load(myStream);
  49.                     //previewing the XML
  50.                     string xml =parsedMyStream.OuterXml;
  51.                     XDocument xDocument = XDocument.Parse(xml);
  52.                     XMLPreview.Text = xDocument.ToString();
  53.                     currentXML = parsedMyStream;  
  54.                     return parsedMyStream;
  55.                 }
  56.                 catch (XmlException ex)
  57.                 {
  58.                     MessageBox.Show("The XML could not be read. " + ex);
  59.                     return CreateEmptyXmlDocument();
  60.                 }
  61.             }
  62.            
  63.            
  64.         }
  65.         public XmlDocument XmlDocument { get; private set; }
  66.        
  67.         private XmlDocument CreateEmptyXmlDocument()
  68.         {
  69.             //  Return an empty XmlDocument if the open file window was closed
  70.             XmlDocument emptyMyStream = new XmlDocument();
  71.             return emptyMyStream;
  72.         }
  73.  
  74.         private void label1_Click(object sender, EventArgs e)
  75.         {
  76.         }
  77.  
  78.         private void Form1_Load(object sender, EventArgs e)
  79.         {
  80.  
  81.         }
  82.  
  83.         private void XMLPreview_TextChanged(object sender, EventArgs e)
  84.         {
  85.            
  86.         }
  87.                
  88.         private void AddDB_Click(object sender, EventArgs e)
  89.         {
  90.             //using DOM approach to parse XML file
  91.             // process XML data using DOM approach      
  92.             XmlDocument invoiceXML = currentXML;
  93.             XmlNodeList invoiceList = invoiceXML.GetElementsByTagName("Invoice");
  94.             XmlNodeList productList = invoiceXML.GetElementsByTagName("Product");
  95.             ArrayList invoices = new ArrayList();
  96.             Invoice invoice = new Invoice();
  97.             foreach (XmlNode node in invoiceList)
  98.             {
  99.                 invoice = new Invoice();
  100.                 invoice.InvoiceID = node.Attributes[0].Value;
  101.                 invoice.InvoiceDate = node.ChildNodes[0].InnerText;
  102.                 invoice.SellerID = node.ChildNodes[1].InnerText;
  103.                 invoice.OrderID = node.ChildNodes[3].InnerText;
  104.                 decimal totalItemPrice = 0;
  105.                 foreach (XmlNode node2 in productList)
  106.                 {
  107.                     decimal qty = Convert.ToDecimal(node.ChildNodes[3].InnerText);
  108.                     decimal unitpx = Convert.ToDecimal(node.ChildNodes[4].InnerText);
  109.                     totalItemPrice += (qty * unitpx);
  110.  
  111.                 }
  112.                 invoice.ItemsTotalPrice = totalItemPrice;
  113.                 invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
  114.                 invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);
  115.                 invoices.Add(invoice);
  116.  
  117.             }
  118.             //insert into DB
  119.             DBManager dbmgr = new DBManager();
  120.             dbmgr.insertInvoice(invoices);
  121.         }
  122.  
  123.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  124.         {
  125.  
  126.         }
  127.  
  128.         private void richTextBox2_TextChanged(object sender, EventArgs e)
  129.         {
  130.  
  131.         }
  132.  
  133.         private void checkInvoiceItem_Click(object sender, EventArgs e)
  134.         {
  135.            
  136.  
  137.         }
  138.  
  139.         private void checkInvoice_Click(object sender, EventArgs e)
  140.         {            
  141.             XmlDocument invoiceXML = currentXML;
  142.             XmlNodeList invoiceList = invoiceXML.GetElementsByTagName("Invoice");
  143.             XmlNodeList productList = invoiceXML.GetElementsByTagName("Product");
  144.             ArrayList invoices = new ArrayList();
  145.             Invoice invoice = new Invoice();
  146.             foreach (XmlNode node in invoiceList)
  147.             {
  148.                 invoice = new Invoice();
  149.                 invoice.InvoiceID = node.Attributes[0].Value;
  150.                 invoice.InvoiceDate = node.ChildNodes[0].InnerText;
  151.                 invoice.SellerID = node.ChildNodes[1].InnerText;
  152.                 invoice.OrderID = node.ChildNodes[3].InnerText;
  153.                 decimal totalItemPrice = 0;
  154.                 foreach (XmlNode node2 in productList)
  155.                 {
  156.                     decimal qty = Convert.ToDecimal(node.ChildNodes[3].InnerText);
  157.                     decimal unitpx = Convert.ToDecimal(node.ChildNodes[4].InnerText);
  158.                     totalItemPrice += (qty * unitpx);
  159.  
  160.                 }
  161.                 invoice.ItemsTotalPrice = totalItemPrice;
  162.                 invoice.ShippingCharges = Convert.ToDecimal(node.ChildNodes[4].InnerText);
  163.                 invoice.InvoiceTotal = Convert.ToDecimal(node.ChildNodes[5].InnerText);
  164.                 invoices.Add(invoice);
  165.  
  166.             }
  167.             StringBuilder sb = new StringBuilder();
  168.             foreach(object obj in invoices)
  169.             {
  170.                 sb.Append(obj);
  171.             }
  172.             richTextBox1.Text = sb.ToString();
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement