CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    21

    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.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

  3. #3
    Join Date
    Dec 2005
    Posts
    21

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Trying to iterate through XML files

    Quote Originally Posted by Markyjj View Post
    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.

  5. #5
    Join Date
    Dec 2005
    Posts
    21

    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)
    {



    }






    }
    }

  6. #6
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    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
    Last edited by ixilom; November 15th, 2009 at 04:29 PM.

  7. #7
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Trying to iterate through XML files

    Quote Originally Posted by Markyjj View Post
    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.

  8. #8
    Join Date
    Dec 2005
    Posts
    21

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured