Reject an Invalid XML File

Hello:

I have the following code, that can read an xml file and display on a console application.

Now, I have tried adding code, that can be called if the xml isnt in the right format..

But for some weird reason, its not working.
Can anyone please take a look at the following code, and let me know what to do, incase the xml file isnt in the right format and should be able to display the message: "Failed to Load File"

THanks.


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

namespace TestUtility
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class EntryPoint
      {
            private bool m_bIsFailure= true ;
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  //checking to see if length of aruguments is less than 1,
                  //therefore displaying  no parameters have been entered
                  if( args.Length < 1 )
                  {
                        Console.WriteLine("No Parameters Have Been Entered");
                        Console.ReadLine();
                  }
                  // this checks to see that # of arguments passed in are 2 ,
                  // This executes the xml content as the # of arguments that we have passed are 2.
                  if( args.Length >0 && args.Length < 3)
                  {
                        if(args.GetValue(0).ToString()== "/xml")
                        {
                              Execute_Cmd_xml(args);
                        }
                        else
                        {
                              Console.WriteLine("Unrecognised command");
                              Console.ReadLine();
                        }
                  }
                  else
                  {
                        //In case the input parameter doesnt have a double quotes, alert to specify that quotes are needed
                        Console.WriteLine("The path must contain double quotes around it");
                        Console.ReadLine();
                        Console.WriteLine("The xml isnt properly formatted"); // new code 4/4/2006 - should be deleted if problems
                  }
            }

            //new code for in proper xml format

            private void ValidationFailed (object sender,ValidationEventArgs args)
            {
                  m_bIsFailure = true;
                  Console.WriteLine("Invalid XML File: " + args.Message);
            }


            //end of in proper xml format
            static void Execute_Cmd_xml(string[] args)
            {
                  string sIFolderName =""; // declare an empty string
                  // If the # of args passed is only 1 instead of 2, alert for a path to be provided.
                  if (args.Length==1)
                  {
                        Console.WriteLine("Please Provide a Path");
                        Console.ReadLine();
                  }
                  try
                  {
                        XmlDocument doc = new XmlDocument();   // create an object
                        sIFolderName=args[1];                  // assigning second argument to the declared variable.
                        //if file exists, load the document,
                        //start selecting each node, and
                        //start with first key or value of 2nd key of each node.
                        if (System.IO.File.Exists(sIFolderName))
                        {
                              doc.Load(sIFolderName);
                              //This is to display the keys and values of the xml file.
                              //Get each key and the corresponding values
                              //using a variable xn
                              foreach (XmlNode xn in doc.SelectNodes("elite_dms/app_settings/add"))
                              {
                                    Console.WriteLine("Key = {0} Value = {1}",xn.Attributes.GetNamedItem("key").Value,xn.Attributes.GetNamedItem("value").Value );
                              }
                              Console.ReadLine();
                        }
                        else
                        {
                              Console.WriteLine("Failed to Load Xml Document: {0}",sIFolderName);
                              Console.ReadLine();
                        }
                  }
                  catch(XmlException xmlEx)
                  {
                        Console.WriteLine(xmlEx.Message);
                        Console.ReadLine();
                  }
            }
      }
}



Reetu2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MogalManicCommented:
Does your XML have a schema?

If so, then the XmlDocument class is not validating from the schema.  To validate, just load a slightly different way:
    XmlDocument doc = new XmlDocument();

    //Load the XML using the
    XmlValidatingReader = new XmlValidatingReader(new XmlTextReader(sIFolderName));

    doc.Load(reader);

This wil throw an exception if the XML does not match the schema associated with the XML filename

If you don't want to be that strict and/or you don't have a schema, then you can just put a count of the node list returned by
  doc.SelectNodes("elite_dms/app_settings/add")
if the count of the nodelist is 0, then throw an exception saying:"There are no 'elite_dms/app_settings/add' node"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

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.