2016-11-22 92 views
0

`使用UnityEngine;使用System.Collections的 ; using System.Collections.Generic; using System.Text;使用System.Xml的 ;使用System.IO的 ;在Unity内部加载XML

public class xmlhandler{ 
public XmlNodeList GetData(string xmlfilepath,string tag) 
{ 
    XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document. 
    TextAsset textAsset = (TextAsset)Resources.Load(xmlfilepath, typeof(TextAsset)); 
    xmlDoc.Load(textAsset.text); // load the file. 
    XmlNodeList data = xmlDoc.GetElementsByTagName(tag); // array of the level nodes. 
    return data; 
} 
} 

`

我似乎无法能够获得XML正确加载。这是正确的路径,当被调用时,我检查了一切。我不确定要提供什么其他信息。我假设我只是没有使用正确的功能或东西。对于不知道发生了什么,感到抱歉。

我得到的错误是

ArgumentException: Illegal characters in path. 
System.IO.Path.IsPathRooted (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:508) 
System.IO.Path.InsecureGetFullPath (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:357) 
System.IO.Path.GetFullPath (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/Path.cs:289) 
System.Xml.XmlResolver.ResolveUri (System.Uri baseUri, System.String relativeUri) 
System.Xml.XmlUrlResolver.ResolveUri (System.Uri baseUri, System.String relativeUri) 
Mono.Xml2.XmlTextReader.GetStreamFromUrl (System.String url, System.String& absoluteUriString) 
Mono.Xml2.XmlTextReader..ctor (System.String url, System.Xml.XmlNameTable nt) 
System.Xml.XmlTextReader..ctor (System.String url, System.Xml.XmlNameTable nt) 
System.Xml.XmlDocument.Load (System.String filename) 
xmlhandler.GetData (System.String xmlfilepath, System.String tag) (at Assets/Resources/scripts/xmlhandler.cs:13) 
structure_mercenarypost.Start() (at Assets/Resources/scripts/structures/structure_mercenarypost.cs:24) 
+1

什么是你给的Xmlfilepath。因为你的错误是说,它包含非法字符。 –

+0

info = xml.GetData(“scripts/structures/structuresxml”,“structure”); –

+0

如果'info'是你通过的路径。然后我可以理解它是失败的。你需要文件路径。 –

回答

0

XmlDocument.Load()接受文件路径。你传入的是你的XML的全部内容(可能包含尖括号,导致这个特定的错误)。

而不是Load(),您要使用LoadXml()。该方法接受您从TextAsset中提取的XML字符串。