简体   繁体   English

使用RestSharp将XML CDATA反序列化为字符串变量

[英]Deserializing XML CDATA into string variable with RestSharp

I'm trying to take an RSS feed and deserialize it into a list of rssEntry objects. 我正在尝试使用RSS feed并将其反序列化为rssEntry对象的列表。

var Client = new RestClient("url here");
Request = new RestRequest { RequestFormat DataFormat.Xml };
var response = Client.Execute<Channel>(Request);
return response.Data.Item;

This fills everything in except content which contains CDATA 这将填充所有内容,但包含CDATA的内容除外

Channel.cs Channel.cs

 public class Channel
 {
    public string Title { get; set; }
    public string Link { get; set; }
    public string AtomLink { get; set; }
    public string Description { get; set; }
    public DateTime LastBuildDate { get; set; }
    public string Generator { get; set; }
    public string Language { get; set; }
    public string UpdatePeriod { get; set; }
    public int UpdateFrequency { get; set; }
    public RssItems Item { get; set; }
}

Item.cs Item.cs

public class Item 
{
        public string Title { get; set; }
        public string Link { get; set; }
        public string Comments { get; set; }
        public DateTime PubDate { get; set; }
        public string Creator { get; set; }
        public string Category { get; set; }
        public string Description { get; set; }
        public string Content { get; set; }
        public string Guid { get; set; }
        public string CommentRss { get; set; }
        public int SlashComments { get; set; }
  }

I'm open to using something other than RestSharp, but I was trying it out for this hoping it would be a nice easy solution. 我愿意使用RestSharp以外的其他工具,但是我为此进行了尝试,希望它是一个不错的简单解决方案。

Currently any field with CDATA is returned as null. 当前,任何带有CDATA的字段都将返回null。

The problem was that I read through the xml in the RSS feed and I named the variables in the items class content. 问题是我通读了RSS feed中的xml,并在项目类内容中命名了变量。 The actual item element in the rss feed was content:encoded. rss feed中的实际item元素是content:encoded。

Changing this variable to Encoded fixed it, completely my own fault. 将此变量更改为Encoded修复了它,完全是我自己的错。

public class Item 
{
        public string Title { get; set; }
        public string Link { get; set; }
        public string Comments { get; set; }
        public DateTime PubDate { get; set; }
        public string Creator { get; set; }
        public string Category { get; set; }
        public string Description { get; set; }
        public string Encoded { get; set; }
        public string Guid { get; set; }
        public string CommentRss { get; set; }
        public int SlashComments { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM