Deserialize XML to object (need to return a list of objects)

heidiwisteria

New Member
Started practicing with XML and C# and I have an error message of "There is an error in XML document (3,2)". After looking at the file, I can't see anything wrong with it (Mind you, I probably missed something since I'm a noob). I'm using a Console Application for C# right now. I'm trying to return a list of Adventurers and just a side note, the GEAR element is optional. Here is what I have so far:XML File - Test1\[code\]<?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>\[/code\]C# Classes\[code\]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; }}\[/code\]Serialize Function - Where the Problem Occurs at Line 5\[code\]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();\[/code\]
 
Back
Top