0

我正在使用 webclient 反序列化来自 web 服务的 XML 内容:

var serializer = new XmlSerializer(typeof(SearchArticles), new XmlRootAttribute("search_articles"));
var results = (SearchArticles)serializer.Deserialize(response.Content.ReadAsStreamAsync().Result);

xml内容如下:

<?xml version=\"1.0\" encoding=\"UTF-8\"?><search_articles><articles hits=\"10134\"><article><id>1763794</id>...

我的模型看起来像:

public class SearchArticles
{
    [XmlElement("articles")]
    public ArticleHelper articles { get; set; }
}

public class ArticleHelper
{
    [XmlAttribute("hits")]
    public long hits { get; set; }
    [XmlElement("article")]
    public List<Article> articles { get; set; }
}

public class Article
{
    public long id { get; set; }
    public string partner { get; set; }
    public string number { get; set; }
    public long number_is_generated { get; set; }
    public string status { get; set; }
    public long company_id { get; set; }
}

这是有效的,但我怎样才能避免拥有 ArticleHelper ?

4

1 回答 1

0

SearchArticles您可以通过修改和删除 ArticleHelper Article

public class SearchArticles
{
    [XmlArray("articles")]
    [XmlArrayItem("article", Type = typeof(Article))]
    public List<Article> articles { get; set; }
}

public class Article
{
    public long id { get; set; }
    public string partner { get; set; }
    public string number { get; set; }
    public long number_is_generated { get; set; }
    public string status { get; set; }
    public long company_id { get; set; }
}

但是,您将无法访问 hits 属性值。

于 2012-09-13T13:54:50.403 回答