4 Replies - 2194 Views - Last Post: 15 March 2012 - 05:06 AM Rate Topic: -----

#1 zeta()  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-March 12

Problem when trying to read an xml file in c#

Posted 13 March 2012 - 05:39 PM

Hi everyone,

It seems to be a pretty easy task but I am stuck.
I have a console application and I need it to read an xml file (I will type the xml file during runtime),it will do some changes to it and then save it as another xml file. (e.g. testapp.exe inputfile.xml outputfile.xml)

I have the following code but when I run it I get an index out of bound on my save part and it doesnt allow me to write anything on the console window.

    public class test
    {
        XmlDocument xdoc = null;
        public void LoadXML(string pathToXML)
        {
            xdoc= new XmlDocument();
            xdoc.Load(pathToXML);
        }
            
//updates key-values etc

 public void Save(string pathToSave)
        {
            if (xdoc != null)
                xdoc.Save(pathToSave);
        }


and in my main where I get the error is:

static void Main(string[] args)
        {
            [color="#FF0000"]string outputpath = args[1];[/color]

            ConfigHelper test = new ConfigHelper();
            test.LoadXML(args[0]);
            test.Save(outputpath);
        } 



Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem when trying to read an xml file in c#

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,359
  • Joined: 02-June 10

Re: Problem when trying to read an xml file in c#

Posted 13 March 2012 - 05:43 PM

My first thought is "work smarter not harder".
If the end goal is to create an object, or load values into a class instance (an object) - then maybe it might be a LOT easier to use serialization and let the the framework do all the heavy lifting for you. Just a thought.


See FAQ # 9, first answer link. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Problem when trying to read an xml file in c#

Posted 14 March 2012 - 11:33 AM

How many command line arguments are you sending when starting the application? You must send at least 2 for your code to work. That first line of code checks second argument. If there isn't a second argument, then it will give the exact error you are getting.
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Problem when trying to read an xml file in c#

Posted 14 March 2012 - 12:14 PM

So, if you're just running this from Visual Studio, that's probably why you're getting the error. You can set the command line arguments to use when debugging from VS by right-clicking the project file, clicking "Properties", going to the Debug tab, and entering them in the Start Options.
Was This Post Helpful? 2
  • +
  • -

#5 zeta()  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-March 12

Re: Problem when trying to read an xml file in c#

Posted 15 March 2012 - 05:06 AM

View PostCurtis Rutland, on 14 March 2012 - 12:14 PM, said:

So, if you're just running this from Visual Studio, that's probably why you're getting the error. You can set the command line arguments to use when debugging from VS by right-clicking the project file, clicking "Properties", going to the Debug tab, and entering them in the Start Options.

I don't know how to mark it but this was the answer to my problem!! Thanks a lot everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1