我正在使用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?
答案 0 :(得分:0)
您可以将SearchArticles
和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属性值的访问权限。