我很難做這個看似簡單的任務。我想加載XML文件與加載藝術資產一樣容易:XNA:加載和讀取XML文件的最佳方法?
content = new ContentManager(Services);
content.RootDirectory = "Content";
Texture2d background = content.Load<Texture2D>("images\\ice");
我不知道如何做到這一點。這tutorial似乎有幫助,但我怎麼得到一個StorageDevice
實例?
我有現在一些工作,但感覺很哈克:
public IDictionary<string, string> Get(string typeName)
{
IDictionary<String, String> result = new Dictionary<String, String>();
xmlReader.Read(); // get past the XML declaration
string element = null;
string text = null;
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element:
element = xmlReader.Name;
break;
case XmlNodeType.Text:
text = xmlReader.Value;
break;
}
if (text != null && element != null)
{
result[element] = text;
text = null;
element = null;
}
}
return result;
}
我將此下面的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<zombies>
<zombie>
<health>100</health>
<positionX>23</positionX>
<positionY>12</positionY>
<speed>2</speed>
</zombie>
</zombies>
而且它能夠通過這個單元測試:
internal virtual IPersistentState CreateIPersistentState(string fullpath)
{
IPersistentState target = new ReadWriteXML(File.Open(fullpath, FileMode.Open));
return target;
}
/// <summary>
///A test for Get with one zombie.
///</summary>
//[TestMethod()]
public void SimpleGetTest()
{
string fullPath = "C:\\pathTo\\Data\\SavedZombies.xml";
IPersistentState target = CreateIPersistentState(fullPath);
string typeName = "zombie";
IDictionary<string, string> expected = new Dictionary<string, string>();
expected["health"] = "100";
expected["positionX"] = "23";
expected["positionY"] = "12";
expected["speed"] = "2";
IDictionary<string, string> actual = target.Get(typeName);
foreach (KeyValuePair<string, string> entry in expected)
{
Assert.AreEqual(entry.Value, expected[entry.Key]);
}
}
目前方法的缺點:文件加載效果不佳,並且鍵與值匹配就像它比必要的更努力。另外,我懷疑這種方法會在XML中出現多個條目而崩潰。
我無法想象這是最佳實施。
UPDATE:繼@Peter Lillevold的建議,我已經改變了這一點:
加載仍然是相當糟糕的,並不知我是不是能夠得到單行ToDictionary
與這兩個lambda一起工作。我不得不求助於那個foreach循環。我在那裏做錯了什麼?
你有一個錯字。我的示例中的@只能與字符串文字一起使用,而不能與字符串變量或參數一起使用。 – 2010-03-14 02:58:24
你知道XNA內容管道ALREADY支持XML文件,不是嗎?因此,可以從字面上使用與加載藝術文件來加載XML文件相同的語法。 – 2011-07-05 05:16:27