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


started practicing xml , c# , have error message of "there error in xml document (3,2)". after looking @ file, can't see wrong (mind you, missed since i'm noob). i'm using console application c# right now. i'm trying return list of adventurers , side note, gear element optional. here have far:

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; } } 

serialize function - problem occurs @ 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(); 

the issue list of adventurers in catalog:

<?xml version="1.0" encoding="utf-8"?> <catalog>     <adventurers> <!-- you're missing -->         <adventurer>         </adventurer>         ...         <adventurer>         </adventurer>     </adventurers> <!-- , missing --> </catalog> 

you don't have wrapping element adventurers collection.

edit: way, find easiest way build xml structure , make sure it's compatible create object(s) in c#, run through built-in xmlserializer , use xml output basis xml create rather forming hand.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -