944,179 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1359
  • C# RSS
Nov 1st, 2009
0

Trying to iterate through XML files

Expand Post »
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.


c# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
markyjj is offline Offline
6 posts
since Sep 2008
Nov 1st, 2009
0
Re: Trying to iterate through XML files
It sounds as if you just need to iterate through files in a directory.
Here is an example of that:
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is offline Offline
1,697 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Tables
Next Thread in C# Forum Timeline: send sms with c#.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC