【问题标题】:Advice on parsing XML document关于解析 XML 文档的建议
【发布时间】: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&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; }
}

【问题讨论】:

  • 您能描述一下您遇到的实际问题吗?
  • 这可能不言而喻,但万智牌卡上的文字和图像是海岸奇才队的版权。我不是律师,但请注意您对 XML 文件的处理方式及其分发方式。
  • 好吧,当 readFile() 方法完成时,列表中没有任何内容。所以我无事可做。
  • 感谢 akton,我确实获得了使用链接的权限,只要我不存储本地副本并在应用程序中提及它们作为来源。

标签: c# xml serialization xml-serialization deserialization


【解决方案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# 中,我们使用 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; }
}

【讨论】:

  • 诅咒,打败我 - 不应该填写我示例中的所有字段。 :)
  • @JerKimball 谢谢 :) 顺便说一句,使用显式转换而不是值属性更安全 - 转换将为不存在的元素返回 null,访问属性会引发异常
  • 工作就像一个魅力。感谢你们俩这么快!
  • 所有天才头脑的问题我最初的问题得到了回答,但现在我想知道是否有任何方法可以让 picURL 脱离集合...
  • (string)c.Element("set").Attribute("picURL")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 2011-06-21
  • 2016-05-29
  • 1970-01-01
相关资源
最近更新 更多