简体   繁体   中英

Windows Phone System.XML.XMLException: Root Element is Missing

Updated I have a fairly large XML document that contains over 20,000 objects. All the sudden its throwing a System.XML.XMLException: Root Element is Missing.

Here are my code blocks:

    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;


    }

Example of my card object in 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>

The issue looks like its with my Deserializer but it worked fine up till last week. :(

So there is definitely something wrong with the XML formatting. It passed the XML validator's, I used three different ones, but I still can not get the older code to work. I used my source control to pull back an older version and it still doesn't run. I am leaning towards blaming Visual Studio now, I am running the 2013 preview and that's the only variable I did not test. I do want to find a way to make the older method work so if anyone has something that hasn't already been tried happy to keep going. This method takes a significant longer time to load in terms of user experience. I did get it working. Here is what I had to resort to as far as reading the 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM