c# program to read xml files in a directory

I I need the C# code to do the following
1.Scan every directory(including sub directories) between 3/15/2011 and now
2.Read any file that starts with "Status....xml
3.Look for the status tag in the XML. Example <Status> </status>
3.Write the directory path,the status to a text file if the status is not <Status>Success</status>
wademiAsked:
Who is Participating?
 
Alfred A.Commented:
Try the following.   Don't forget to add System.IO and System.Xml.  I hope this helps.
using System.IO;
using System.Xml;

private void Form1_Load(object sender, EventArgs e)
{    
    string[] filenames = Directory.GetFiles(@"C:\YourDir", "Status*.xml", SearchOption.AllDirectories);
    foreach (string p in filenames)
    {
       FileInfo fi = new FileInfo(p);
       string StatusStr = string.Empty;
       DateTime dt;
       bool isOK = DateTime.TryParse("03/15/2011",out dt);
       if((fi.LastWriteTime.CompareTo(dt) >= 0) && (fi.LastWriteTime.CompareTo(DateTime.Now) <= 0))
       {
           //Read XML File
           XmlDocument doc = new XmlDocument();
           doc.Load(p);
           XmlNodeList elemList = doc.GetElementsByTagName("Status");
           for (int i = 0; i < elemList.Count; i++)
           {
               StatusStr = elemList[i].InnerXml;
           };
           if (StatusStr != "Success")
           {
               File.AppendAllText(@"C:\Test\result.txt", p);
               File.AppendAllText(@"C:\Test\result.txt", StatusStr);
           }
       }
    }
}

Open in new window

0
 
Fernando SotoRetiredCommented:
This question is a duplicate post that has been already asnwered here; http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26943392.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.