【发布时间】:2013-01-02 02:14:18
【问题描述】:
希望在这方面得到一些帮助:我非常感谢一位开发人员与我分享了这个 XML 文件,因为它可以让我省去很多麻烦。但他告诉我,如何阅读它是我自己的事。基本上我正在为纸牌游戏编写一个 Windows 商店应用程序。我有一个 XML,它是我的卡片列表,并希望将其读入列表。我没有收到任何错误,并且之前已将 XML 读入列表。任何建议将不胜感激。
这是一个 XML 的 sn-p:
<?xml version="1.0" encoding="UTF-8"?><carddatabase>
<cards>
<card>
<name>Tundra Kavu</name>
<set picURL="http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=26805&type=card" picURLHq="" picURLSt="">AP</set>
<color>R</color>
<manacost>2R</manacost>
<type>Creature - Kavu</type>
<pt>2/2</pt>
<tablerow>2</tablerow>
<text>{T}: Target land becomes a Plains or an Island until end of turn.</text>
</card>
</cards>
</carddatabase>
这是我的序列化器:
public async void readFile()
{
StorageFolder myFolder = ApplicationData.Current.LocalFolder;
StorageFile myFile = await myFolder.CreateFileAsync("cards.xml", CreationCollisionOption.OpenIfExists);
XmlSerializer Serializer = new XmlSerializer(typeof(List<card>), new XmlRootAttribute("carddatabase"));
string XmlString = await FileIO.ReadTextAsync(myFile);
XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(myFile);
var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Auto, IgnoreWhitespace = true, IgnoreComments = true };
var stringReader = new StringReader(XmlString);
XmlReader reader = XmlReader.Create(stringReader, settings);
List<card> temp = (List<card>)Serializer.Deserialize(reader);
foreach(card x in temp)
{
await tempTable.InsertAsync(x);
}
}
这是我的卡片类:
public class card
{
public string id { get; set; }
public string name { get; set; }
public string manacost { get; set; }
public string set { get; set; }
public string color { get; set; }
public string tablerow { get; set; }
public string text { get; set; }
public string type { get; set; }
}
【问题讨论】:
-
您能描述一下您遇到的实际问题吗?
-
这可能不言而喻,但万智牌卡上的文字和图像是海岸奇才队的版权。我不是律师,但请注意您对 XML 文件的处理方式及其分发方式。
-
好吧,当 readFile() 方法完成时,列表中没有任何内容。所以我无事可做。
-
感谢 akton,我确实获得了使用链接的权限,只要我不存储本地副本并在应用程序中提及它们作为来源。
标签: c# xml serialization xml-serialization deserialization