Trying to iterate through XML files
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.
Re: Trying to iterate through XML files
So what I understand is that you already have code that will traverse through a XML file, what you are looking for now is a way to traverse through the files of a particular. You should use System.IO.Directory.GetFiles() method to loop through each file and then use the existing method for traversing the XML file.
Re: Trying to iterate through XML files
Thanks for the reply, I have tried the approach that you specified but it just reads through the one xml file and then that's it. Any futher assistance would be of a great help
Re: Trying to iterate through XML files
Quote:
Originally Posted by
Markyjj
Thanks for the reply, I have tried the approach that you specified but it just reads through the one xml file and then that's it. Any futher assistance would be of a great help
Post your code that makes the GetFiles() call.
Re: Trying to iterate through XML files
Hi again, sorry for the delayed reply, but please find below the code containing the GetFiles() call.
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
}
// 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)
{
}
}
}
Re: Trying to iterate through XML files
---EDIT---
Err... I just saw you had an foreach loop there,
But why are you refering to a single char in the fileName?
I think you meant to write
Code:
doc.Load(fileName);
---ORIGNAL POST---
You are specifically just reading one file with this
Code:
doc.Load(string.Format( "",fileName[1]));
What you need is a foreach loop :)
Code:
foreach(string oneFile in fileName)
{
doc.Load(string.Format( "",oneFile));
}
Sidenote, when declaring arrays, make it a habit to name them in plural, fileName becomes fileNames :)
Re: Trying to iterate through XML files
Quote:
Originally Posted by
Markyjj
Hi again, sorry for the delayed reply, but please find below the code containing the GetFiles() call.
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");
}
Also you are opening only one file result001.xml always with xmlreader but you need to loop through the XFile string array list and open one by one.
Re: Trying to iterate through XML files
thanks for that, I have now used 'doc.Load(fileName);' in the code to load all the records into the Xml document. Do I now need to loop through all the nodes in the Xml document
Thanks