Iterating through Xml files

trying to iterate through XML files in folder

Page 1 of 1

9 Replies - 7656 Views - Last Post: 15 November 2009 - 12:21 PM Rate Topic: -----

#1 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Iterating through Xml files

Posted 01 November 2009 - 09:55 AM

I would like ask for some advice please. 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.

Many thanks


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace VoteProg_1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		int XFileCount = 0;
		DataSet Ds = new DataSet();
	   
		XmlTextReader XReader = new XmlTextReader("C:\\Users\\Mark\\Documents\\VoteResults2\\result001.xml");
	   
	
		


		private void label1_Click(object sender, EventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		
		{
		 

			foreach (string fileName in XFiles)
			{				
			while (XReader.Read()) 
			{
				switch (XReader.NodeType) 
				{
					case XmlNodeType.Element: // The node is an element.
						textBox1.Text = XReader.Name.ToString();						
						break;
					case XmlNodeType.Text: //Display the text in each element.
						textBox1.Text = XReader.Value.ToString();
						break;
					case XmlNodeType.EndElement: //Display the end of the element.
						 textBox1.Text = XReader.Name.ToString();						
						break;
				}
			}				
			
		}
}


		 private void label13_Click(object sender, EventArgs e)
		{

		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void button2_Click(object sender, EventArgs e)
		{
			
					

		}






	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Iterating through Xml files

#2 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Iterating through Xml files

Posted 01 November 2009 - 10:26 AM

Are all the fildes in one directory? Why not just use a foreach on the files in the directory? Or if you know how many just a for loop.
Was This Post Helpful? 0
  • +
  • -

#3 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: Iterating through Xml files

Posted 01 November 2009 - 11:05 AM

View Postblank_program, on 1 Nov, 2009 - 09:26 AM, said:

Are all the fildes in one directory? Why not just use a foreach on the files in the directory? Or if you know how many just a for loop.

Hi, thanks for the reply. All the Xml files are in 1 directory and I have tried a foreach loop, but am unsure of how to loop through each XML file and checking the values inside. As I have already tried the foreach loop with the Getfiles(), but this just gets the name of the file. Also, the XML reader only reads a specific Xml file path. I would be grateful of any methods I could use that actually search deeper in each xml file. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Iterating through Xml files

Posted 01 November 2009 - 12:43 PM

First of all, what is XFiles in your case?

As a solution for your problem, I would suggest you explicitly specifiying the folder, and then just add the file name at the end for proper iteration. Something like this:

foreach (string fileName in fileNameArray)
{
XmlDocument doc = new XmlDocument();
doc.Load (string.Format("C:\\SomeFolder\{0}", fileName));
// Add other code here
}



fileNameArray is obviously the array that contains the file names.
Was This Post Helpful? 0
  • +
  • -

#5 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: Iterating through Xml files

Posted 01 November 2009 - 01:37 PM

XFiles is the string array that I used to store the Xml file names, which I was then trying to iterate through with the Foreach loop but I failed badly. thanks for your help and I will implement your code and hope that it works. I will add further if I have any more problems if that's ok. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: Iterating through Xml files

Posted 04 November 2009 - 06:32 PM

View Postmarkyjj, on 1 Nov, 2009 - 12:37 PM, said:

XFiles is the string array that I used to store the Xml file names, which I was then trying to iterate through with the Foreach loop but I failed badly. thanks for your help and I will implement your code and hope that it works. I will add further if I have any more problems if that's ok. Thanks again.

I have tried implementing the above code into my program but when I put the file path and the fileName parameter into the format.Load method I get the following error message 'The given path's format is not supported'. This is only shown when I run the program and press the button that has the button handler with this method.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Iterating through Xml files

Posted 04 November 2009 - 09:16 PM

View Postmarkyjj, on 4 Nov, 2009 - 09:32 PM, said:

View Postmarkyjj, on 1 Nov, 2009 - 12:37 PM, said:

XFiles is the string array that I used to store the Xml file names, which I was then trying to iterate through with the Foreach loop but I failed badly. thanks for your help and I will implement your code and hope that it works. I will add further if I have any more problems if that's ok. Thanks again.

I have tried implementing the above code into my program but when I put the file path and the fileName parameter into the format.Load method I get the following error message 'The given path's format is not supported'. This is only shown when I run the program and press the button that has the button handler with this method.


can you post the code the gives the error message?
Was This Post Helpful? 0
  • +
  • -

#8 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: Iterating through Xml files

Posted 06 November 2009 - 02:20 PM

View Posteclipsed4utoo, on 4 Nov, 2009 - 08:16 PM, said:

View Postmarkyjj, on 4 Nov, 2009 - 09:32 PM, said:

View Postmarkyjj, on 1 Nov, 2009 - 12:37 PM, said:

XFiles is the string array that I used to store the Xml file names, which I was then trying to iterate through with the Foreach loop but I failed badly. thanks for your help and I will implement your code and hope that it works. I will add further if I have any more problems if that's ok. Thanks again.

I have tried implementing the above code into my program but when I put the file path and the fileName parameter into the format.Load method I get the following error message 'The given path's format is not supported'. This is only shown when I run the program and press the button that has the button handler with this method.


can you post the code the gives the error message?


Please find below the code that I use to implement the method for selecting the XMl files in the folder.

 private void button1_Click(object sender, EventArgs e)
		{
			foreach (string fileName in XFiles)
			{
				XmlDocument doc = new XmlDocument();
				doc.Load (string.Format("C:\\Users\\Mark\\Documents\\VoteResults2"\\{0}", fileName));

				
			}   
	  
		} 
		   




VoteResults2 is where the xml files are

Many thanks
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Iterating through Xml files

Posted 07 November 2009 - 06:35 AM

where are you populating "XFiles"?
Was This Post Helpful? 0
  • +
  • -

#10 markyjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-August 09

Re: Iterating through Xml files

Posted 15 November 2009 - 12:21 PM

View Posteclipsed4utoo, on 7 Nov, 2009 - 05:35 AM, said:

where are you populating "XFiles"?


Hi again, sorry about the late reply.....below is the full code that I am using at the moment showing where 'XFiles' is populated.


using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace VoteProg_1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
	 
		string[] XFiles = Directory.GetFiles("C:\\Users\\Mark\\Documents\\VoteResults2", "*.xml");
		XmlTextReader XReader = new XmlTextReader("C:\\Users\\Mark\\Documents\\VoteResults2\\result001.xml");
	   
	
		


		private void label1_Click(object sender, EventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		{
			foreach (string fileName in XFiles)
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(string.Format( "",fileName[1]));
				// Add other code here





			}		 


		}
		
		private void label13_Click(object sender, EventArgs e)
		{

		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void button2_Click(object sender, EventArgs e)
		{
			
					

		}






	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1