나는이 겉보기에 간단한 작업을하는 데 어려움을 겪고 있습니다. 아트 애셋을로드하는 것과 마찬가지로 쉽게 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는 :
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]);
}
}
로딩은 여전히 꽤 엉터리이고, 어떻게 든 내가 원 -을 얻을 수 없습니다 : @ 피터 Lillevold의 조언에 따라, 나는이 조금 변경했습니다 라인 ToDictionary
그 두 람다와 함께 작동합니다. 나는 그 foreach 루프에 의지해야했다. 내가 뭘 잘못하고 있니?
거기에 오타가 있습니다. @ from my sample은 문자열 변수 나 매개 변수가 아닌 문자열 리터럴에서만 사용할 수 있습니다. –
XNA 콘텐츠 파이프 라인이 이미 XML 파일을 지원한다는 사실을 알고 있습니까? 따라서 XML 파일을로드하기 위해 아트 파일을로드하는 데 사용하는 것과 동일한 구문을 문자 그대로 사용할 수 있습니다. –