Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello friends,

I am trying to retrieve and pass only directory elements from my xml file to the error watcher as string with a filter. But then i pass the found path by name into the error watcher as string. I get an exception. The code directly moves onto the catch block. Any help would be appreciated.

Thank in advance.

What I have tried:

public string[] Create(string settingsFile)
            {
                string FilePath = settingsFile;
                settingsFile = "my path/directory of the xml file";
                string[] a = new string[20];

                XmlTextReader xmlReader = new XmlTextReader(settingsFile);
                try
                {
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.Name)
                        {
                            case "InputDataPath":
                                a[0] = xmlReader.ReadString();
                                ErrorWatcher(a[0], "Status.error");
                                continue;
                            case "BackupDataPath":
                                a[1] = xmlReader.ReadString();
                                ErrorWatcher(a[1], "Status.error");
                                continue;
                            case "ExportDatapath":
                                a[2] = xmlReader.ReadString();
                                ErrorWatcher(a[2], "Status.error");
                                continue;
                        }

                    }

                }
                catch (Exception e)
                {
                    return null;
                }
                return a;
                
            }
Posted
Updated 21-Nov-17 20:24pm
v3
Comments
Karthik_Mahalingam 19-Nov-17 11:18am    
what is the error message?
Muthu Raman 19-Nov-17 11:26am    
I does not give any specific error msg karthik. Directly goes onto the catch block without executing the method after receiving the values.
Karthik_Mahalingam 19-Nov-17 11:29am    
put a break point inside catch block and see the value in "e"
Muthu Raman 19-Nov-17 17:25pm    
I had to change the "\\" to "\". Issue is solved!!!
Thank you karthik.
Karthik_Mahalingam 19-Nov-17 22:24pm    
welcome

1 solution

Start by using the debugger: Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Start by looking at the path you pass in: if it's a folder, that won't work.
If that looks ok - and the file exists - step on through until the error happens. Then look at the exception detail to find out why it excepted - there is usually some very helpful info in there! Probably, the file is bad and the XML reader is throwing an exception outside your try block.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And do yourself a couple of favours: Log exception, don't just ignore them and return null - when you do that, you have no idea what the error was, so it's hard to fix.
And close your XML reader when you are finished with it (a using block is the simplest way) - if you don't it remains open until the garbage collector get around to closing it for you or your app exits, whichever comes first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900