Unity + C#:XML-path导致FormatException

时间:2014-11-12 16:54:16

标签: c# xml unity3d formatexception

我是Unity和C#的新手,所以不要和我一起努力;)

我正在尝试读取包含一些配置数据的xml文件(xsd有效,用VS2012生成* .cs),我得到的格式异常显然与xml文件的路径有关。 “test”-field是xml-file-content的主要类。

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

XMLLoaderSaver具有以下代码:

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;
} ...

显然问题是解析路径。 经度:

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)

我真的不明白如何解决这个问题,但也许有人可以帮助我。

谢谢!

编辑:

我的xml文件基本上是这样的:

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

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

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

我已经检查了xsd并对其进行了验证,所以这不应该是问题。

将文件路径更改为“。\ Assets \ xml \ sample.xml”不会更改异常。

1 个答案:

答案 0 :(得分:0)

您的XML不包含任何元素的值,只包含其名称。从您向我们显示的错误中,其中一个字段应该是一个整数,但是不存在数字数据。如果我尝试将空字符串解析为整数,如下所示:

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

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

我得到了同样的例外:

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

您没有向我们展示您的课程,因此我们不知道应该存在哪些值,但是如果字段e应该是数字,则XML应该类似于:

<e>23</e>

您应该确定XML文件不包含实际数据的原因;这可能是你的问题。

如果这不是导致问题的实际XML,您应该向我们展示您尝试阅读它的类的实际XML和骨架。