在Element标记内反序列化cdata

时间:2015-01-14 23:21:42

标签: c# deserialization cdata

反序列化的新功能,我正在尝试反序列化RSS提要,并在读取xml文件时在描述CData字段上给出错误。不确定如何处理元素标记内的cdata。感谢您对此的任何帮助...

示例数据Feed

<rss version="2.0">
   <channel>
      <title>Elite: Dangerous Galnet News</title>
      <link>http://www.elitedangerous.com/galnet/rss</link>
      <language>en-us</language>
      <item>
         <title>Federal President Declares Plant an Illegal Narcotic</title>
         <description><![CDATA[Federal President Declares Plant an Illegal Narcotic

         The Federal President Jasmina Halsey, has declared the newly discovered rare narcotic ‘Onionhead’ illegal throughout Federal space. Onionhead is the fruit of an alien plant, only grown on Panem  in the independent Kappa Fornacis system.  Halsey has said she is concerned by the effect it is having on Federal youth in that part of the Galaxy. "We must be strong and protect our youth from vile substances that are rotting their minds. This is why I have taken this difficult step."]]></description>
         <pubDate>Thu, 16 Dec 3300 00:00:00 UTC</pubDate>
      </item>
      <item>
         <title>Slave Rebellion in Sorbago</title>
         <description><![CDATA[Slave Rebellion in Sorbago 

         Rumours of the slave rebellion in the Sorbago system have been confirmed. In a speech to business leaders, pro-slavery Senator Zemina Torval denied sending two of her personal Majestic Class Interdictors, the Boudica and the Elizabeth, to put down the revolt, but said "They have gone to Sorbago to help the local mining corporation keep the peace and to keep out the external forces that are trying to create a conflict".  Reports suggested the slaves are not being treated according to Imperial Law. Torval has announced generous payments to those that help her, and has criticised the barbaric treatment of unregulated slaves outside the Empire in the past. "I will personally sponsor any unregulated slaves brought to Synteini, Shinigami, Quile, LTT 9810, Tau-2 Gruis A, Naunei, and Tepertsi to be given full Imperial Rights and treated as Imperial Slaves."]]></description>
         <pubDate>Thu, 16 Dec 3300 00:00:00 UTC</pubDate>
      </item>
      <item>
         <title>Farmer’s Leader Accuses President of Victimisation</title>
         <description><![CDATA[Farmer’s Leader Accuses President of Victimisation    

         Georgio Algeria, the spokesman for the Farmer’s Union in the Kappa Fornacis system has spoken out at the Federal President’s declaration that Onionhead, the main export from the Kappa Fornacis system, be illegal.  At a press conference on Panem he said “Onionhead is great, cool. Loved across the galaxy. We make it, you eat it. Don’t eat it if you don’t want it. Call yourself President! We will keep making it and you will keep eating it. It’s what we do. It’s all we do.”  Algeria would not be drawn on it in public, but it is clear the President's controversial actions have spread publicity for Onionhead far and wide and demand is higher than ever on the black market.]]></description>
         <pubDate>Fri, 17 Dec 3300 00:00:00 UTC</pubDate>
      </item>
      </channel>
</rss>

以下是我尝试反序列化的代码:

    #region Galnet News

    public static rss RSSFeed = new rss();

    #region classes

    [Serializable]
    public class rss
    {
         [XmlElement("channel")]
        public List<Channel> Channel { get; set; }
    }

    [Serializable]
    public class Channel
    {
        [XmlElement("title")]
        public string Title { get; set; }

        [XmlElement("link")]
        public string Link { get; set; }

        [XmlElement("language")]
        public string Language { get; set; }

        [XmlElement("item")]
        public List<RSSItems> RssItems { get; set; }
    }

    [Serializable]
    public class RSSItems
    {
        private DateTime pubDate;

        [XmlElement("title")]
        public string Title { get; set; }

        [XmlElement("description")]
        [XmlText]
        public string Description { get; set; } 

        [XmlElement("pubDate")]
        public DateTime PubDate 
        {
            get { return pubDate; }
            set
            {
               pubDate = Convert.ToDateTime(value.ToString());
            } 
        }

        public bool Read { get; set; }

    }

    #endregion


    public static List<Channel> Galnet_Serialization_Read(string path, bool loadDBFromFile)
    {

        try
        {
            RSSFeed = new rss();

            var reader = new XmlSerializer(RSSFeed.GetType());

            // Read the XML file.
            var file = new StreamReader(@path);

            // Deserialize the content of the file into a Book object.
            RSSFeed = (rss)reader.Deserialize(file);

        }
        catch (Exception error)
        {
            MessageBox.Show("error: " + error.Message + " File: " + @path);
        }

        return RSSFeed.Channel;

    }

也试过这个替换没有成功..相同的错误:

        private string description;
        [XmlElement("description")]
        public XmlCDataSection Description
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                return doc.CreateCDataSection(description);
            }
            set
            {
                description = value.Value;
            }
        }

以下是实际XML文件的链接:https://dl.dropboxusercontent.com/u/20107089/GalnetNews.xml

0 个答案:

没有答案