将XML反序列化为Object始终为null

时间:2014-02-06 20:38:01

标签: c# xml xml-serialization

我似乎无法理解这一点。我试图将一些xml反序列化为一个对象,我的测试结果为负,因为在所有属性中返回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;

            }
        }
    }

更新:我试过吐两个班,但是 我不太明白这两个类是如何联系的。我的示例下面返回CurrencyList对象,但里面的列表没有项目。

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

    }

1 个答案:

答案 0 :(得分:1)

您的类与XML的结构不匹配。你应该有两个类,一个用于<currencies_response>,另一个用于<currency>中的嵌套<currencies>元素 - 这可能表示一系列元素({ {1}}在C#中使用List<Currency>