我在完成这个看似简单的任务时遇到了困难.我想加载XML文件,同样容易加载艺术资产:
content = new ContentManager(Services);
content.RootDirectory = "Content";
Texture2d background = content.Load<Texture2D>("images\\ice");
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做.本教程似乎很有帮助,但我如何获得StorageDevice
实例?
我现在有一些工作,但感觉非常hacky:
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;
}
Run Code Online (Sandbox Code Playgroud)
我将其应用于以下XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<zombies>
<zombie>
<health>100</health>
<positionX>23</positionX>
<positionY>12</positionY>
<speed>2</speed>
</zombie>
</zombies>
Run Code Online (Sandbox Code Playgroud)
它能够通过这个单元测试:
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
当前方法的缺点是:文件加载效果不佳,并且将值与值匹配似乎比不必要的更省力.此外,我怀疑这种方法会因XML中的多个条目而分崩离析.
我无法想象这是最佳实现.
更新:根据@Peter Lillevold的建议,我改变了一点:
public IDictionary<string, string> Get(string typeName)
{
IDictionary<String, String> result = new Dictionary<String, String>();
IEnumerable<XElement> zombieValues = root.Element(@typeName).Elements();
//result["health"] = zombie.Element("health").ToString();
IDictionary<string, XElement> nameToElement = zombieValues.ToDictionary(element => element.Name.ToString());
foreach (KeyValuePair<string, XElement> entry in nameToElement)
{
result[entry.Key] = entry.Value.FirstNode.ToString();
}
return result;
}
public ReadWriteXML(string uri)
{
root = XElement.Load(uri);
}
internal virtual IPersistentState CreateIPersistentState(string fullpath)
{
return new ReadWriteXML(fullpath);
}
/// <summary>
///A test for Get with one zombie.
///</summary>
[TestMethod()]
public void SimpleGetTest()
{
IPersistentState target = CreateIPersistentState("../../../path/Data/SavedZombies.xml");
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, actual[entry.Key]);
}
}
Run Code Online (Sandbox Code Playgroud)
装载仍然非常糟糕,不知怎的,我无法让这一行ToDictionary
与这两个lambdas一起工作.我不得不求助于那个foreach循环.我在那里做错了什么?
还有新的闪亮的XElement(将Linq转换为XML).此示例将加载xml文件,查找僵尸并将值转储到字典中:
var doc = XElement.Load("filename");
var zombieValues = doc.Element("zombie").Elements();
var zombieDictionary =
zombieValues.ToDictionary(
element => element.Name.ToString(),
element => element.Value);
Run Code Online (Sandbox Code Playgroud)
如果您更愿意明确选择每个值(并使用强制转换为自动转换为正确的值类型),您可以执行以下操作:
var zombie = doc.Element("zombie");
var health = (int)zombie.Element("health");
var positionX = (int)zombie.Element("positionX");
var positionY = (int)zombie.Element("positionY");
var speed = (int)zombie.Element("speed");
Run Code Online (Sandbox Code Playgroud)
更新:修复一些拼写错误并清理一下,你的Get
方法应如下所示:
public IDictionary<string, string> Get(string typeName)
{
var zombie = root.Element(typeName);
return zombie.Elements()
.ToDictionary(
element => element.Name.ToString(),
element => element.Value);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15084 次 |
最近记录: |