解析XML文档的建议

时间:2013-01-02 02:14:19

标签: c# xml serialization xml-serialization deserialization

希望能得到一些帮助:我非常感谢开发人员与我共享这个XML文件,因为它应该让我省去很多麻烦。但他告诉我,我自己就是如何阅读它。基本上我正在为一个纸牌游戏写一个Windows商店应用程序。我有一个XML是我的卡列表,并希望将其读入列表。我没有错误,并且之前已将XML读入列表。任何建议将不胜感激。

以下是XML片段:

<?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&amp;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; }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Linq解析xml:

XDocument xdoc = XDocument.Load(myFile);
var cards = from c in xdoc.Descendants("card")
            select new card() {
                name = (string)c.Element("name"),
                manacost = (string)c.Element("manacost"),
                set = (string)c.Element("set"),
                color = (string)c.Element("color"),
                tableRow = (string)c.Element("tablerow"),
                text = (string)c.Element("text"),
                type = (string)c.Element("type")
            };

foreach(var card in cards)
    await tempTable.InsertAsync(card); 

Linq还允许您将值从字符串转换为其他数据类型,因此您可以在类中使用属​​性int TableRow { get; set; },可以将其解析为TableRow = (int)c.Element("tablerow")(如果是tablerow元素,则为int?不是必需的。)

在C#中BTW我们使用CamelCase名称来表示类型和属性。因此,请考虑使用类型:

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 int TableRow { get; set; }
    public string Text { get; set; }
    public string Type { get; set; }
}