gpt4 book ai didi

c# - XNA:加载和读取 XML 文件的最佳方式?

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:18 25 4
gpt4 key购买 nike

我很难完成这个看似简单的任务。我想像加载艺术 Assets 一样轻松地加载 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 中的多个条目而失效。

我无法想象这是最佳实现。

更新:根据@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]);
}
}

加载仍然很糟糕,不知何故我无法让单行 ToDictionary 与这两个 lambda 一起工作。我不得不求助于那个 foreach 循环。我在那里做错了什么?

最佳答案

还有新的 Shiny 的XElement (哪个运动Linq to XML)。此示例将加载一个 xml 文件,查找僵尸并将值转储到字典中:

var doc = XElement.Load("filename");
var zombieValues = doc.Element("zombie").Elements();
var zombieDictionary =
zombieValues.ToDictionary(
element => element.Name.ToString(),
element => element.Value);

如果您更愿意明确选择每个值(并使用转换自动转换为适当的值类型),您可以这样做:

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");

更新:修复了一些拼写错误并做了一些清理,您的 Get 方法应该如下所示:

public IDictionary<string, string> Get(string typeName)
{
var zombie = root.Element(typeName);
return zombie.Elements()
.ToDictionary(
element => element.Name.ToString(),
element => element.Value);
}

关于c# - XNA:加载和读取 XML 文件的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2439636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com