Unity + C#: XML-path leads to FormatException

562 views Asked by At

I'm new to Unity and C# so dont be to hard with me ;)

I'm trying to read a xml-file that contains some configuration data (xsd valid, *.cs generated with VS2012) and I am getting a FormatException that apparently has to do with the path of the xml-file. The "test"-field is the main class for the xml-file-content.

    xmlLoader = new XMLLoaderSaver ();
    print ("before read");
    test = xmlLoader.readIt ("./Assets/xml/sample.xml");
    print ("after read");
    test.ToString ();

The XMLLoaderSaver has the following code:

public class XMLLoaderSaver {

public leberDefense readIt(string path){

    var serializer = new XmlSerializer (typeof(leberDefense));
    var stream = new FileStream (path, FileMode.Open, FileAccess.Read);
    var container = serializer.Deserialize (stream) as leberDefense;
    stream.Close();
    return container;
} ...

Apparently the problem is some parsing of the path. Log:

before read
UnityEngine.MonoBehaviour:print(Object)
GameController:Start() (at Assets/scripts/GameController.cs:37)


FormatException: Input string was not in the correct format

System.Int32.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:640)
System.Xml.XmlConvert.ToInt32 (System.String s)
System.Xml.Serialization.XmlCustomFormatter.FromXmlString (System.Xml.Serialization.TypeData type, System.String value)
System.Xml.Serialization.XmlSerializationReaderInterpreter.GetValueFromXmlString (System.String value, System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlTypeMapping typeMap)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadPrimitiveValue (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot ()
System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader)

I dont really understand how to solve this issue, but maybe someone can help me.

Thanks!

edit:

my xml file basically looks like this:

<root>
    <elem1>
        <s1/>
        <s2/>
        <s3/>
    </elem1>

    <elem2>
      <w>
        <e/>
        <e/>
        <e/>
      </w>

      <w>
        <e/>
        <e/>
        <e/>
      </w>
    </elem2>
</root>

i have checked the xsd and validated the file against it, so that should not be the problem.

changing the file path to ".\Assets\xml\sample.xml" does not change the exception.

1

There are 1 answers

1
dbc On BEST ANSWER

Your XML contains no values for any of the elements, just their names. From the error you are showing us, one of the fields is supposed to be an integer, however there is no numeric data present. If I try to parse an empty string into an integer, like so:

System.Xml.XmlConvert.ToInt32(string.Empty)

or

int.Parse(string.Empty, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture)

I get the same exception:

'System.Xml.XmlConvert.ToInt32(string.Empty)' threw an exception of type 'System.FormatException'
    base {System.SystemException}: {"Input string was not in a correct format."}

You don't show us your classes so we don't know what values ought to be present, however if the field e is supposed to be numeric, the XML should look something like:

<e>23</e>

You should determine why your XML file contains no real data; this is probably your problem.

If this is not the actual XML causing the problem, you should show us the actual XML, and skeletons of the classes into which you are trying to read it.