Iniziato a praticare con XML e C# e ho un messaggio di errore "C'è un errore nel documento XML (3,2)". Dopo aver guardato il file, non riesco a vedere nulla di sbagliato in esso (attenzione, probabilmente mi sono perso qualcosa dato che sono un noob). Sto usando un'applicazione console per C# in questo momento. Sto provando a restituire una lista di avventurieri e solo una nota a margine, l'elemento GEAR è facoltativo. Ecco quello che ho finora:Deserialize XML to object (è necessario restituire un elenco di oggetti)
file XML - 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>
classi C#
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 Funzione - in cui il problema si verifica in Linea 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();
Si prega di vedere, "[Se le domande specifiche della lingua contengono il nome della lingua nel titolo?] (Http://meta.stackexchange.com/questions/19190/should-language-specific-questions-contain-the-language -name-in-the-title) ", dove il consenso è" no, non dovrebbero ". –