Trying to iterate through XML files

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: markyjj is an unknown quantity at this point 
Solved Threads: 0
markyjj markyjj is offline Offline
Newbie Poster

Trying to iterate through XML files

 
0
  #1
Nov 1st, 2009
I would like ask for some advice please, I am trying to create a program in C# but have the following problem. I am currently trying to loop through a batch of XML files that are stored in a folder. However, I just don't want to loop through the XML file name, as I need to check through all the nodes in each XML file(650 files) for certain values. I know how to loop through a specific Xml file but unsure of how to actually loop through all XMl files to retrieve the values inside the values. For now, all I have is code to read through a specific XML file as shown below. I would be grateful for any assistance in this matter.


  1.  
  2. using System;
  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.Windows.Forms;
  10. using System.Xml;
  11. using System.IO;
  12. using System.Xml.Linq;
  13.  
  14. namespace VoteProg_1
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23.  
  24. XmlTextReader XReader = new XmlTextReader ("C:\\Users\\Mark\\Documents\\VoteResults2\\result001.xml");
  25.  
  26. private void label1_Click(object sender, EventArgs e)
  27. {
  28.  
  29. }
  30.  
  31. private void button1_Click(object sender, EventArgs e)
  32.  
  33. {
  34.  
  35. foreach (string fileName in XFiles)
  36. {
  37.  
  38.  
  39. while (XReader.Read())
  40. {
  41. switch (XReader.NodeType)
  42. {
  43. case XmlNodeType.Element: // The node is an element.
  44. textBox1.Text = XReader.Name.ToString();
  45. break;
  46. case XmlNodeType.Text: //Display the text in each element.
  47. textBox1.Text = XReader.Value.ToString();
  48. break;
  49. case XmlNodeType.EndElement: //Display the end of the element.
  50. textBox1.Text = XReader.Name.ToString();
  51. break;
  52. }
  53. }
  54. }
  55.  
  56. // textBox1.Text = XmlStr.ToString();
  57.  
  58.  
  59. }
  60. }
  61.  
  62. private void label13_Click(object sender, EventArgs e)
  63. {
  64.  
  65. }
  66.  
  67. private void Form1_Load(object sender, EventArgs e)
  68. {
  69.  
  70. }
  71.  
  72. private void button2_Click(object sender, EventArgs e)
  73. {
  74.  
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. }
  85. }
Last edited by peter_budo; Nov 2nd, 2009 at 7:25 pm. Reason: Correcting code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
Nov 1st, 2009
It sounds as if you just need to iterate through files in a directory.
Here is an example of that:
  1. using System;
  2. using System.IO;
  3.  
  4. namespace DW_FileLoop
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string strRootDir = @"C:\Users\Mark\Documents\VoteResults2";
  11. string[] arr_strFiles = Directory.GetFiles(strRootDir, "*.xml");
  12.  
  13. foreach (string strFile in arr_strFiles)
  14. {
  15. Console.WriteLine("Processing: " + strFile);
  16. //Call XML Processor here.
  17. }
  18. }
  19. }
  20. }
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C# Forum


Views: 294 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC