Windows Phone System.XML.XMLException:根元素缺失

时间:2013-07-09 17:50:10

标签: c# xml windows-phone-8 deserialization

更新 我有一个相当大的XML文档,包含超过20,000个对象。它突然抛出一个System.XML.XMLException:根元素丢失了。

以下是我的代码块:

    public async Task<int> readDatabase()
    {

        IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication();
        List<card> temp= new List<card>();
        XmlSerializer s = new XmlSerializer(typeof(List<card>), new XmlRootAttribute("card"));
        using (TextReader reader = new StreamReader(myFile.OpenFile("cards.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
        {
               cardList = (List<card>)s.Deserialize(reader);
        }
        return 1;


    }

我的XML卡对象示例

    <?xml version="1.0" encoding="UTF-8"?>
    <mtg_carddatabase>
    <bdd_date><![CDATA[5/1/2013]]></bdd_date>
    <bdd_version><![CDATA[1.00]]></bdd_version>
    <bdd_name>Magic the Gathering® Card Database</bdd_name>
    <bdd_editor>Wizards of the Coast LLC</bdd_editor>
    <sets>
    <set>
    <name><![CDATA[15th Anniversary]]></name>
    <code><![CDATA[15A]]></code>
    <code_magiccards><![CDATA[15ANN]]></code_magiccards>
    <date><![CDATA[00/0000]]></date>
    <is_promo><![CDATA[True]]></is_promo>
    </set>
    </sets>
    <cards>
    <card>

    <id><![CDATA[3064]]></id>

    <name><![CDATA[Urza's Engine]]></name>

    <set><![CDATA[AL]]></set>

    <type><![CDATA[Artifact Creature — Juggernaut]]></type>

    <rarity><![CDATA[U]]></rarity>

    <manacost><![CDATA[{5}]]></manacost>

    <converted_manacost><![CDATA[5]]></converted_manacost>

    <power><![CDATA[1]]></power>

    <toughness><![CDATA[5]]></toughness>

    <loyalty></loyalty>

    <ability><![CDATA[Trample£{3}: Urza's Engine gains banding until end of turn. #_(Any 
    creatures with banding, and up to one without, can attack in a band. Bands are blocked as a 
    group. If any creatures with banding you control are blocking or being blocked by a 
    creature, you divide that creature's combat damage, not its controller, among any of the 
    creatures it's being blocked by or is blocking.)_#£{3}: Attacking creatures banded with 
    Urza's Engine gain trample until end of turn.]]></ability>

    <flavor><![CDATA[#_"Humans and machines working together can be fearsome indeed"Arcum 
    Dagsson_#]]></flavor>

    <variation></variation>

    <artist><![CDATA[Greg Simanson]]></artist>

    <number><![CDATA[180]]></number>

    <rating><![CDATA[2.449]]></rating>

    <ruling><![CDATA[£10/1/2008 : If a creature with banding attacks, it can team up with any 
    number of other attacking creatures with banding (and up to one nonbanding creature) and 
    attack as a unit called a "band." The band can be blocked by any creature that could block a 
    single creature in the band. Blocking any creature in a band blocks the entire band. If a 
    creature with banding is blocked, the attacking player chooses how the blockers' damage is 
    assigned. £10/1/2008 : A maximum of one nonbanding creature can join an attacking band no 
    matter how many creatures with banding are in it. £10/1/2008 : Creatures in the same band 
    must all attack the same player or planeswalker. £10/1/2009 : If a creature in combat has 
    banding, its controller assigns damage for creatures blocking or blocked by it. That player 
    can ignore the damage assignment order when making this assignment.]]></ruling>

    <color><![CDATA[A]]></color>

    <generated_mana></generated_mana>

    <pricing_low><![CDATA[0,08]]></pricing_low>

    <pricing_mid><![CDATA[0,25]]></pricing_mid>

    <pricing_high><![CDATA[1,01]]></pricing_high>

    <back_id></back_id>

    <watermark></watermark>

    <name_CN><![CDATA[]]></name_CN>

    <name_TW><![CDATA[]]></name_TW>

    <name_FR><![CDATA[Locomotive d'Urza]]></name_FR>

    <name_DE><![CDATA[Urzas Maschine]]></name_DE>

    <name_IT><![CDATA[Motrice di Urza]]></name_IT>

    <name_JP><![CDATA[]]></name_JP>

    <name_PT><![CDATA[Engenho de Urza]]></name_PT>

    <name_RU><![CDATA[]]></name_RU>

    <name_ES><![CDATA[Ingenio de Urza]]></name_ES>

    <name_KO><![CDATA[]]></name_KO>

    <legality_Block><![CDATA[b]]></legality_Block>

    <legality_Standard><![CDATA[b]]></legality_Standard>

    <legality_Extended><![CDATA[b]]></legality_Extended>

    <legality_Modern><![CDATA[b]]></legality_Modern>

    <legality_Legacy><![CDATA[v]]></legality_Legacy>

    <legality_Vintage><![CDATA[v]]></legality_Vintage>

    <legality_Highlander><![CDATA[v]]></legality_Highlander>

    <legality_French_Commander><![CDATA[v]]></legality_French_Commander>

    <legality_Commander><![CDATA[v]]></legality_Commander>

    <legality_Peasant><![CDATA[u]]></legality_Peasant>

    <legality_Pauper><![CDATA[b]]></legality_Pauper>

    </card>
    </cards>
    </mtg_carddatabase>

这个问题看起来和我的Deserializer一样,但它在上周工作正常。 :(

1 个答案:

答案 0 :(得分:0)

因此XML格式肯定存在问题。它通过了XML验证器,我使用了三个不同的验证器,但我仍然无法使旧的代码工作。我使用我的源代码控制来取回旧版本但它仍然无法运行。我现在倾向于责备Visual Studio,我正在运行2013预览版,这是我没有测试的唯一变量。我确实想找到一种方法来使旧方法工作,所以如果有人有一些尚未尝试继续前进的东西。在用户体验方面,此方法需要花费更长的时间来加载。我确实让它运转了。就阅读XML而言,这是我不得不求助的。

    try
        {
            XDocument xdoc = XDocument.Load("cards.xml");
            var cards = from c in xdoc.Descendants("card")
                        select new card()
                        {
                            Nname = (string)c.Element("name"),
                            Nmanacost = (string)c.Element("manacost"),
                            Nset = (string)c.Element("set"),
                            Ncolor = (string)c.Element("color"),
                            Nid = (string)c.Element("id"),
                            Nability = (string)c.Element("text"),
                            Ntype = (string)c.Element("type"),
                            Npricing_high = (string)c.Element("pricing_high"),
                            Npricing_low = (string)c.Element("pricing_low"),
                            Npricing_mid = (string)c.Element("pricing_mid"),
                            Nartist = (string)c.Element("artist"),
                            Nrarity = (string)c.Element("rarity"),
                            Nback_id = (string)c.Element("back_id"),
                            Nconverted_manacost = (string)c.Element("converted_manacost"),
                            Nflavor = (string)c.Element("flavor"),
                            Ngenerated_mana = (string)c.Element("generated_mana"),
                            Nlegality_Block = (string)c.Element("legality_Block"),
                            Nlegality_Commander = (string)c.Element("legality_Commander"),
                            Nlegality_Extended = (string)c.Element("legality_Extended"),
                            Nlegality_French_Commander = (string)c.Element("legality_French_Commander"),
                            Nlegality_Highlander = (string)c.Element("legality_Highlander"),
                            Nlegality_Legacy = (string)c.Element("legality_Legacy"),
                            Nlegality_Modern = (string)c.Element("legality_Modern"),
                            Nlegality_Pauper = (string)c.Element("legality_Pauper"),
                            Nlegality_Peasant = (string)c.Element("legality_Peasant"),
                            Nlegality_Vintage = (string)c.Element("legality_Vintage"),
                            Nlegality_Standard = (string)c.Element("legality_Standard"),
                            Nloyalty = (string)c.Element("loyalty"),
                            Nname_CN = (string)c.Element("name_CN"),
                            Nname_DE = (string)c.Element("name_DE"),
                            Nname_ES = (string)c.Element("name_ES"),
                            Nname_FR = (string)c.Element("name_FR"),
                            Nname_IT = (string)c.Element("name_IT"),
                            Nname_JP = (string)c.Element("name_JP"),
                            Nname_KO = (string)c.Element("name_KO"),
                            Nname_PT = (string)c.Element("name_PT"),
                            Nname_RU = (string)c.Element("name_RU"),
                            Nname_TW = (string)c.Element("name_TW"),
                            Nnumber = (string)c.Element("number"),
                            Npower = (string)c.Element("power"),
                            Nrating = (string)c.Element("rating"),
                            Nruling = (string)c.Element("ruling"),
                            Ntoughness = (string)c.Element("toughness"),
                            Nvariation = (string)c.Element("variation"),
                            Nwatermark = (string)c.Element("watermark")
                            //Url = (string)c.Element("set").Attribute("picURL")
                        };
                            cardList = cards.ToList();
        }
        catch (Exception e)
        {
            string error = e.Message;
        }
        return 1;