- Home /
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 ) <Missions> <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.
Answer by DaveA · Aug 30, 2011 at 07:08 PM
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"?> ??
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('>');
X$$anonymous$$LNode currentNode = this;
X$$anonymous$$LNodeList currentNodeList = null;
bool list$$anonymous$$ode = false;
object ob;
for (int i = 0; i < bits.Length; i++)
{
if (list$$anonymous$$ode)
{
currentNode = (X$$anonymous$$LNode)currentNodeList[int.Parse(bits[i])];
ob = currentNode;
list$$anonymous$$ode = false;
}
else
{
ob = currentNode[bits[i]];
if (ob is ArrayList)
{
currentNodeList = (X$$anonymous$$LNodeList)(ob as ArrayList);
list$$anonymous$$ode = 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 (list$$anonymous$$ode)
return currentNodeList;
else
return currentNode;
}[/code]
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 X$$anonymous$$L libraries right there for you, in System.Xml.
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 ins$$anonymous$$d of the third-party one.
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. :)
Your answer
Follow this Question
Related Questions
Invalid Encoding Specification Xml 1 Answer
Float.Parse question 1 Answer
FormatException: Input string was not in the correct format - headache 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers