Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public partial class selectPOSReports : Form
  2. {
  3. XmlDocument doc = new XmlDocument();
  4.  
  5. public selectPOSReports()
  6. {
  7. InitializeComponent();
  8.  
  9. if (!File.Exists(@"C:UsersPublicMECposReportList.xml"))
  10. {
  11. Directory.CreateDirectory(@"C:UsersPublicMEC");
  12. doc.LoadXml("<?xml version="1.0" encoding="utf-8"?><Reports count="0"></Reports>");
  13. doc.Save(@"C:UsersPublicMECposReportList.xml");
  14.  
  15. }
  16. else
  17. {
  18. doc.Load(@"C:UsersPublicMECposReportList.xml");
  19. }
  20.  
  21. XmlNodeList excelReportList = doc.SelectNodes("//Workbook");
  22.  
  23. foreach (XmlNode excelReport in excelReportList)
  24. {
  25. reportList.Items.Add(excelReport.InnerText);
  26. }
  27. }
  28.  
  29. private void addButton_Click(object sender, EventArgs e)
  30. {
  31. int newIndex = 0;
  32. if ( selectReportDialog.ShowDialog() == DialogResult.OK ) {
  33. doc.Save(@"C:UsersPublicMECposReportList.xml");
  34. string fileName = selectReportDialog.FileName;
  35. string filePath = Path.GetPathRoot(fileName);
  36. XDocument xd = XDocument.Load(XmlReader.Create(@"C:UsersPublicMECposReportList.xml"));
  37. xd.Element("Reports").Add(
  38. new XElement("Report", new XAttribute("id", newIndex),
  39. new XElement("Workbook", fileName),
  40. new XElement("Filepath", filePath)));
  41. xd.Save(@"C:UsersPublicMECposReportList.xml");
  42. reportList.Items.Add(fileName);
  43. }
  44. }
  45.  
  46. private void removeButton_Click(object sender, EventArgs e)
  47. {
  48. DialogResult result = MessageBox.Show("Are you sure you want to remove " + reportList.SelectedItems.ToString() + " from the list?",
  49. "Remove Excel Report",
  50. MessageBoxButtons.YesNo,
  51. MessageBoxIcon.Warning);
  52.  
  53. if (result == DialogResult.Yes)
  54. {
  55. int index = reportList.SelectedIndex;
  56. string sindex = index.ToString();
  57. XmlNode deleteNode = doc.GetElementById(sindex);
  58. deleteNode.ParentNode.RemoveChild(deleteNode);
  59. doc.Save("posReportList.xml");
  60. reportList.Items.RemoveAt(index);
  61. }
  62. }
  63. }
  64.  
  65. <Reports count="1"><!--Count will be updated as items are added-->
  66. <Report id="1">
  67. <Workbook>SomeBook.xlsx</Workbook>
  68. <Filepath>C:/SomePath</FilePath>
  69. </Report>
  70. </Reports>
  71.  
  72. doc.Save(@"C:UsersPublicMECposReportList.xml");
  73.  
  74. xd.Save(@"C:UsersPublicMECposReportList.xml");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement