Your classes don't match the XML's structure. You should have two classes, one for <currencies_response>
and another one for the nested <currency>
element within <currencies>
— that's probably an indication of a sequence of elements (List<Currency>
with [XmlArray]
in C#).
Deserializing XML to Object always null
-
08-10-2022 - |
Domanda
I can not seem to get my head around this. I am trying to deserialize some xml to an object and my test comes out negative, as in all properties return null.
[XmlType(AnonymousType = true, Namespace = "http://cakemarketing.com/api/1/")]
[XmlRoot(Namespace = "http://cakemarketing.com/api/1/", IsNullable = false, DataType = "xml", ElementName = "currencies_response")]
public class Currency
{
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_id")]
public string Id { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_symbol")]
public string Symbol { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_name")]
public string Name { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_abbr")]
public string Abbr { get; set; }
}
[Test]
public void TestMethod1()
{
var testString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " +
"<currencies_response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://cakemarketing.com/api/1/\"> " +
"<success>true</success> " +
"<row_count>7</row_count> " +
"<currencies> <currency> " +
"<currency_id xmlns=\"API:id_name_store\">1</currency_id> " +
"<currency_symbol xmlns=\"API:id_name_store\">$</currency_symbol> " +
"<currency_name xmlns=\"API:id_name_store\">US Dollar</currency_name> " +
"<currency_abbr xmlns=\"API:id_name_store\">USD</currency_abbr>" +
"</currency></currencies>" +
"</currencies_response>";
var t = Deserialize<Currency>(testString);
Assert.IsTrue(!String.IsNullOrEmpty(t.Id));
}
public static T Deserialize<T>(string xml) where T : class, new()
{
var deserializer = new XmlSerializer(typeof(T));
using (var stringReader = new StringReader(xml))
{
using (var xmlReader = XmlReader.Create(stringReader))
{
var list = (T)deserializer.Deserialize(xmlReader);
return list;
}
}
}
UPDATE: I have tried to spit into two classes but I don't quite understand how the two classes relate. my example below returns the CurrencyList object but the list inside has no items.
[XmlType(AnonymousType = true, Namespace = "http://cakemarketing.com/api/1/")]
[XmlRoot(Namespace = "http://cakemarketing.com/api/1/", IsNullable = false, ElementName = "currencies_response")]
public class CurrencyList
{
public List<Currency> Currencies { get; set; }
}
[XmlRoot("currencies")]
public class Currency
{
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_id")]
public string Id { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_symbol")]
public string Symbol { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_name")]
public string Name { get; set; }
[XmlElement(Namespace = "API:id_name_store", ElementName = "currency_abbr")]
public string Abbr { get; set; }
}
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow