Destroy the XML for the object (you need to return a list of objects)

The practice will start with XML and C #, and I have the error message "In an XML document (3.2)." Looking at the file, I see nothing wrong with that (of course, I probably missed something because I'm noob). I am using a console application for C # right now. I am trying to return a list of Adventurers and only a side note, the GEAR element is optional. Here is what I still have:

XML File - Test1

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
    <Adventurer>
        <ID>001</ID>
        <Name>John Smith</Name>
        <Address>123 Fake Street</Address>
        <Phone>123-456-7890</Phone>
        <Gear>
            <Attack>
                <Item>
                    <IName>Sword</IName>
                    <IPrice>15.00</IPrice>
                </Item> 
                <Item>
                    <IName>Wand</IName>
                    <IPrice>20.00</IPrice>
                </Item>         
            </Attack>
            <Defense>
                <Item>
                    <IName>Shield</IName>
                    <IPrice>5.00</IPrice>
                </Item>
        </Defense>  
        </Gear>
    </Adventurer>
    <Adventurer>
        <ID>002</ID>
        <Name>Guy noone likes</Name>
        <Address>Some Big House</Address>
        <Phone>666-666-6666</Phone>
        <Gear></Gear>
    </Adventurer>
</Catalog>

C # Classes

public class Catalog
{
    List<Adventurer> Adventurers { get; set; }
}

public class Adventurer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public Gear Gear { get; set; }
}

public class Gear
{
    public List<Item> Attack { get; set; }
    public List<Item> Defense { get; set; }
}

public class Item
{
    public string IName { get; set; }
    public decimal IPrice { get; set; }
}

Serialization Function - Where the Problem Occurs on Line 5

Catalog obj = null;
string path = @"C:\Users\Blah\Desktop\test1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Catalog));
StreamReader reader = new StreamReader(path);
obj = (Catalog)serializer.Deserialize(reader);
reader.Close();

Console.ReadLine();
+5
source share
4 answers

The problem is the list of Adventurers in the Directory:

<?xml version="1.0" encoding="utf-8"?>
<Catalog>
    <Adventurers> <!-- you're missing this -->
        <Adventurer>
        </Adventurer>
        ...
        <Adventurer>
        </Adventurer>
    </Adventurers> <!-- and missing this -->
</Catalog>

You do not have a wrapping element for the collection Adventurers.

EDIT: , XML , , #, XmlSerializer XML XML, , .

+3

-, "Adventurers" , , , - , XML .

+2

XML ... ...

public string City { get; set; }

<Address>123 Fake Street</Address>

, .

: , , ...

<Adventurers> <Catalog> ( </Adventurers> </Catalog>)

List<Adventurer> Adventurers { get; set; }

public List<Adventurer> Adventurers { get; set; }

.

+1

xml ( , Adventurer).

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace TempSandbox
{
    [XmlRoot]
    public class Catalog
    {
        [XmlElement("Adventurer")]
        public List<Adventurer> Adventurers;

        private readonly static Type[] myTypes = new Type[] { typeof(Adventurer), typeof(Gear), typeof(Item) };
        private readonly static XmlSerializer mySerializer = new XmlSerializer(typeof(Catalog), myTypes);

        public static Catalog Deserialize(string xml)
        {
            return (Catalog)Utils.Deserialize(mySerializer, xml, Encoding.UTF8);
        }
    }

    [XmlRoot]
    public class Adventurer
    {
        public int ID;

        public string Name;

        public string Address;

        public string Phone;

        [XmlElement(IsNullable = true)]
        public Gear Gear;
    }

    [XmlRoot]
    public class Gear
    {
        public List<Item> Attack;

        public List<Item> Defense;
    }

    [XmlRoot]
    public class Item
    {
        public string IName;

        public decimal IPrice;
    }
}

I use [XmlElement ("Adventurer")] because the xml element names do not exactly match the class property names.

NOTE. I use the universal deserialization utility that I already had on hand.

+1
source

All Articles