Search code examples
c#arraysforeachxml-parsingxmldocument

Parsing multiple xml files using xmldocument c#


I am able to load multiple xml docs from my c drive but I need to find a way to put something into a loop so I can parse all of the xml's rather than just the first one.

using System;
using System.IO;
using System.Xml;

public class Program
{


    public static void Main()
    {

        string[] filePaths = Directory.GetFiles(@"C:\Users\r626890\Documents\BoxTesting\",      "*.xml");
        foreach (string files in filePaths)
        Console.WriteLine("'{0}'",files);
        Console.WriteLine();

        XmlDocument doc = new XmlDocument();
        string file = filePaths[0].ToString();
        XmlNodeList xmlnode;
        doc.Load(file);

        XmlNodeList indexValue = doc.GetElementsByTagName("Index");

        for (int i = 0; i < indexValue.Count; i++)
        {
            Console.WriteLine();
            Console.WriteLine("Description: {0} ",             indexValue[i].ChildNodes.Item(0).InnerXml);
            Console.WriteLine("Value:       {0} ", indexValue[i].ChildNodes.Item(1).InnerXml);
        //  Console.WriteLine("{0}",i);
        }

        xmlnode = doc.GetElementsByTagName("FileName");
        for (int i = 0; i < xmlnode.Count; i++)
        {
            // Console.WriteLine("URI: {0} ", elemList[i].InnerXml.Split('_')[0]);
            Console.WriteLine();
            Console.WriteLine("Type:        {0} ", xmlnode[i].InnerXml.Split('_')[1]);
            Console.WriteLine();
            Console.WriteLine("File Name:   {0} ", xmlnode[i].InnerXml);
                             Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("(Total # of files: {0})", ++i);
        }



        Console.ReadLine();

    }
}

Solution

  • You have forget the scope of :

    foreach (string files in filePaths)
    

    And you must load your document with :

    doc.Load(files);
    

    Instead of

    doc.Load(file);
    

    Because file is referenced to

    string file = filePaths[0].ToString();
    

    Therefore it's referenced to the first item of the collection filePaths (the first file)

    I suggest you to rename "files" to "file" because it's not a collection, it's just one iterated items of your collection.

        string[] filePaths = Directory.GetFiles(@"C:\Users\r626890\Documents\BoxTesting\",      "*.xml");
        foreach (string file in filePaths)
        {
            Console.WriteLine("'{0}'",file);
            Console.WriteLine();
    
            XmlDocument doc = new XmlDocument();
            //string file = filePaths[0].ToString();
            XmlNodeList xmlnode;
            doc.Load(file);
    
            XmlNodeList indexValue = doc.GetElementsByTagName("Index");
    
            for (int i = 0; i < indexValue.Count; i++)
            {
                Console.WriteLine();
                Console.WriteLine("Description: {0} ",             indexValue[i].ChildNodes.Item(0).InnerXml);
                Console.WriteLine("Value:       {0} ", indexValue[i].ChildNodes.Item(1).InnerXml);
            //  Console.WriteLine("{0}",i);
            }
    
            xmlnode = doc.GetElementsByTagName("FileName");
            for (int i = 0; i < xmlnode.Count; i++)
            {
                // Console.WriteLine("URI: {0} ", elemList[i].InnerXml.Split('_')[0]);
                Console.WriteLine();
                Console.WriteLine("Type:        {0} ", xmlnode[i].InnerXml.Split('_')[1]);
                Console.WriteLine();
                Console.WriteLine("File Name:   {0} ", xmlnode[i].InnerXml);
                                 Console.WriteLine("------------------------------------------------------------");
                Console.WriteLine("(Total # of files: {0})", ++i);
            }
    
        }
    
        Console.ReadLine();