x


xml : FormatException: Input string was not in the correct format

I'm having a bit of trouble when trying to use xml files for loading ( and eventually saving aswell ) Haven't worked with .xml files earlier so I'm kinda clueless.

Anyways, I'm using the Lightweight xml parser created by Fraser McCormick, but I found a version of it through google that was converted into C# since that's the language I'm using.

What I'm doing atm is a mission-system kind of thing, I start with a Level class ( that derieves from Monobehaviour, the other classes does not! ) in which I specify which xml to read and the amount of missions of the level, for each mission I create a new Mission class at runtime, the Mission class is set up to read how many objectives the mission contain and create a new Objective class for each of them.

My problem is when I'm trying to grab data from the xml. I get the error FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) XMLNode.GetObject (System.String path) (at Assets/xml/XMLNode.cs:32) XMLNode.GetValue (System.String path) (at Assets/xml/XMLNode.cs:17) Mission.SetupMission () (at Assets/Code/Quest/Mission.cs:55) LevelMiLogic.SetupMissionLogic () (at Assets/Code/Quest/LevelMiLogic.cs:27) LevelMiLogic.Start () (at Assets/Code/Quest/LevelMiLogic.cs:17)

I press the error in the console and the XMLNode class from Lightweight pops open, highlighting this : `currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])];

from the GetObject method : ` private object GetObject(string path) { string[] bits = path.Split('>'); XMLNode currentNode = this; XMLNodeList currentNodeList = null; bool listMode = false; object ob;

    for (int i = 0; i < bits.Length; i++)
    {
        if (listMode)

         {

            currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])];

            ob = currentNode;
         listMode = false;
        }
        else
        {
         ob = currentNode[bits[i]];

         if (ob is ArrayList)
         {
          currentNodeList = (XMLNodeList)(ob as ArrayList);
          listMode = true;
         }
         else
         {
          // reached a leaf node/attribute
          if (i != (bits.Length - 1))
          {
              // unexpected leaf node
              string actualPath = "";
              for (int j = 0; j <= i; j++)
              {
                 actualPath = actualPath + ">" + bits[j];
              }

              //Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath);
          }

          return ob;
         }
        }
    }

    if (listMode) 
       return currentNodeList;
    else 
       return currentNode;
}`

However... considering the fact that it's the first time I'm playing around with the xml thingies I believe it's more likely something with my code that's wrong so here's the method in my Mission class that's somehow probably causing the problem:P (Posted the stuff above just in case neways) : ` private void SetupMission() {

    //We want to read an xml file and load all necessary data from it

    //if no xml file matching the requested name (playername.ToString() + Levelname.ToString() + .xml)

    //we create a new one from the default



    XMLParser parser = new XMLParser();

    _myLogic.xmlNode = parser.Parse( _myLogic.xmlFile.text );



    //_miName = _myLogic.xmlNode.GetValue( "Missions>Mission>Mi" + _miIndex + ">MiName" );

    //Debug.Log( _miName );

    Debug.Log( _myLogic.xmlNode.GetValue( "Missions>Mission>Mi0>MiName" ));

}`

oh! and the .xml file is like this ( only the beginning of it since that's the only part I'm using atm ) `

<Mission>

    <Mi0>

       <MiName>"MissionOne"</MiName>`

:)I'ld really appreciate some help with this, and if someone knows of a good site where I can read a little further on how to use .xml with c# I'ld appreciate if that person could share it aswell. Have looked through MSDN quickly but I didn't get very much clarity from it.

more ▼

asked Aug 30 '11 at 06:07 PM

Westerlund gravatar image

Westerlund
3 3 3 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

If this is what's complaining:

currentNodeList[int.Parse(bits[i])];

then use MonoDev to debug it, set a breakpoint there (or let it fail there) and examine what's in bits[i]. I don't know why it's trying to parse an int, your xml has none. Perhaps it expects the standard xml header that looks like <?xml version="1.0" encoding="UTF-8"?> ??

more ▼

answered Aug 30 '11 at 07:08 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

No that doesn't seem to be the problem. However in my .xml document I tried pressing the tab Xml>Validate and the console show an error "The attribute use must be optional ( or absent ) if standard attribute is used.

I find it a little weird tho', it says the error was found on Line 44, however my document only has 28 lines right now.

I looked a little in the xml-node file by the way, it looks like the method I'm calling is trying to return the result of int.parse as a string?

[code] public string GetValue(string path) { return GetObject(path) as string; }

private object GetObject(string path)
{
    string[] bits = path.Split('>');
    XMLNode currentNode = this;
    XMLNodeList currentNodeList = null;
    bool listMode = false;
    object ob;

    for (int i = 0; i < bits.Length; i++)
    {
        if (listMode)

         {

            currentNode = (XMLNode)currentNodeList[int.Parse(bits[i])];

            ob = currentNode;
         listMode = false;
        }
        else
        {
         ob = currentNode[bits[i]];

         if (ob is ArrayList)
         {
          currentNodeList = (XMLNodeList)(ob as ArrayList);
          listMode = true;
         }
         else
         {
          // reached a leaf node/attribute
          if (i != (bits.Length - 1))
          {
              // unexpected leaf node
              string actualPath = "";
              for (int j = 0; j <= i; j++)
              {
                 actualPath = actualPath + ">" + bits[j];
              }

              //Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath);
          }

          return ob;
         }
        }
    }

    if (listMode) 
       return currentNodeList;
    else 
       return currentNode;
}[/code]
Sep 02 '11 at 12:47 PM Westerlund

I don't mean to be a spoilsport, but I find it odd that you'd want to use a third-party library in the first place, since .Net already has one of the world's most powerful XML libraries right there for you, in System.Xml.

Sep 02 '11 at 12:58 PM CHPedersen

As I said, I'm not familiar with xml things, and quite new to c# aswell so the reason why I didn't use .net thingie was simply 'cause I didn't really understand it very well and I happened to find a quick walkthrough on how to use that tp parser. Anyways thanks for your comment, it made me put some effort into reading up on how to use System.Xml and I finally managed to get it working that way :) And I'm kinda ashamed to admit that I found it way easier to use the .net's already built in libraries instead of the third-party one.

Sep 08 '11 at 01:36 PM Westerlund

Don't be ashamed to admit that! :) You took initiative and read up on a piece of technology, found out how it worked and why it might be better suited for you, then applied it with success. There's stuff to be proud of in that, not ashamed. :)

Sep 08 '11 at 01:43 PM CHPedersen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4139
x255
x128
x33
x6

asked: Aug 30 '11 at 06:07 PM

Seen: 1780 times

Last Updated: Sep 08 '11 at 01:44 PM