Marco Patritti

Marco Patritti

  • NA
  • 2
  • 632

VS C# - Dump XML data into Access db table

Oct 9 2017 4:20 PM
Hello,
 
I am trying to read data from an XML file and insert it into Access db table.
 
According to my online research, it is best to dump the XML data into a Dataset and then iterate through this dataset to insert the values in the table. Is this recommended or is there an easier way?
 
My current code is below. can see the values (DateCreated, DateModified, etc) written to console when I run the code. Somehow if I use MessageBox.Show() instead of Console.WriteLine() to see the value in a pop-up message box it does not show the value. The message box appears empty.
 
If someone can help me reading those values and inserting them into a new dataset, and then iterate through the dataset to insert into table I would greatly appreciate it.
 
Code:
  1. using (XmlTextReader reader = new XmlTextReader("c:\\XML\\MyTest.xml"))  
  2. {  
  3. while (reader.Read())  
  4. {  
  5. if (reader.IsStartElement())  
  6. {  
  7. switch (reader.Name)  
  8. {  
  9. case "File":  
  10. Console.WriteLine();  
  11. break;  
  12. case "DateCreated":  
  13. Console.WriteLine("DateCreated: " + reader.ReadString());  
  14. break;  
  15. case "DateModified":  
  16. Console.WriteLine("DateModified: " + reader.ReadString());  
  17. break;  
  18. case "Extension":  
  19. Console.WriteLine("Extension: " + reader.ReadString());  
  20. break;  
  21. case "FullPath":  
  22. Console.WriteLine("FullPath: " + reader.ReadString());  
  23. break;  
  24. case "ID":  
  25. Console.WriteLine("ID: " + reader.ReadString());  
  26. break;  
  27. case "Name":  
  28. Console.WriteLine("Name: " + reader.ReadString());  
  29. break;  
  30. case "Size":  
  31. Console.WriteLine("Size: " + reader.ReadString());  
  32. break;  
  33. }  
  34. }  
  35. }  
  36. }  
This is my XML file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Files TotalFiles="3">  
  3. <File>  
  4. <DateCreated>2017-08-18T09:34:51</DateCreated>  
  5. <DateModified>2017-08-18T09:34:51</DateModified>  
  6. <Extension>pdf </Extension>  
  7. <FullPath>Apex Clean Energy Management / Apex Clean Energy / Apex Clean Energy / Land / Lease Sig & Pmt Approvals (KP & MG) / Lease Approval Request Form 081717 v1 - signed.pdf</FullPath>  
  8. <ID>3cbd2086-7377-4bf2-8458-371206287876</ID>  
  9. <Name>Lease Approval Request Form 081717 v1 - signed.pdf</Name>  
  10. <Size>1247062</Size>  
  11. </File>  
  12. <File>  
  13. <DateCreated>2017-08-18T09:34:38</DateCreated>  
  14. <DateModified>2017-08-18T09:34:38</DateModified>  
  15. <Extension>pdf </Extension>  
  16. <FullPath>Apex Clean Energy Management / Apex Clean Energy / Apex Clean Energy / Land / Lease Sig & Pmt Approvals (KP & MG) / Lease Approval Request Form 080317 v1 - signed.pdf</FullPath>  
  17. <ID>fe6a8aa3-aa4f-4587-9220-371206286456</ID>  
  18. <Name>Lease Approval Request Form 080317 v1 - signed.pdf</Name>  
  19. <Size>1074533</Size>  
  20. </File>  
  21. <File>  
  22. <DateCreated>2017-08-18T09:34:24</DateCreated>  
  23. <DateModified>2017-08-18T09:34:24</DateModified>  
  24. <Extension>pdf </Extension>  
  25. <FullPath>Apex Clean Energy Management / Apex Clean Energy / Apex Clean Energy / Land / Lease Sig & Pmt Approvals (KP & MG) / Lease Approval Request Form 072717 v1 - signed.pdf</FullPath>  
  26. <ID>d3cd9f63-257f-4122-b7c4-371206284987</ID>  
  27. <Name>Lease Approval Request Form 072717 v1 - signed.pdf</Name>  
  28. <Size>879380</Size>  
  29. </File>  
  30. </Files>  

Answers (1)