Małgorzata B. ***
Temat: XML deserializacja dla klasy pochodnej
Poniższego XML-a chcę deserializować:
<something id=""234"">
<items>
<item name=""Obj1"">
<a>1</a>
<b>2</b>
</item>
<item name=""Obj2"">
<c>5</c>
<d>2</d>
<e>1</e>
</item>
</items>
</something>
Klasy opisujące XML-a wyglądają jak poniżej:
[XmlRoot("something")]
public class Something: ClassBase
{
[XmlAttribute("id")]
public string id;
[XmlArray("items")]
public ItemsCollection forms { set; get; }
public Something(): base() { }
}
[XmlType("item")]
public class ItemsCollection: CollectionClassBase<Item>
{
public ItemsCollection() : base() { }
}
[XmlType("item")]
public class Item: ClassBase
{
[XmlAttribute("name"), DefaultValue("")]
public string name { set; get; }
}
Do tego momentu dane pobierają mi się poprawnie.
W konkretnym item są różne pola, a itemy rozróżnię po atrybucie name, więc tworzę klasę, która dziedziczy po klasie item (ItemObj1 oraz ItemObj2, poniżej kod dla ItemObj1):
[XmlType("item")]
public class ItemObj1 : Item
{
[XmlElement("a"), DefaultValue("")]
public string a { set; get; }
[XmlElement("b"), DefaultValue("")]
public string b { set; get; }
}
W jaki sposób pobrać dane do ItemObj1?
ClassBase - ustawia wartości domyśle
CollectionClassBase<T> - zapewnia dostęp do listyTen post został edytowany przez Autora dnia 17.03.15 o godzinie 10:30