C#反序列化Atom XML

时间:2018-08-29 07:50:45

标签: c# xml deserialization atom-feed

我想反序列化以下Atom XML:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:tel="http://tel.search.ch/api/spec/result/1.0/" xml:lang="de">
   <id>https://tel.search.ch/api/ed14c7f5e42390640730cb18523640d5</id>
   <title type="text">tel.search.ch API Search Results</title>
   <generator version="1.0" uri="https://tel.search.ch">tel.search.ch</generator>
   <updated>2018-08-28T02:00:00Z</updated>
   <link href="https://tel.search.ch/result.html?was=0418553553" rel="alternate" type="text/html" />
   <link href="http://tel.search.ch/api/?was=0418553553" type="application/atom+xml" rel="self" />
   <openSearch:totalResults>1</openSearch:totalResults>
   <openSearch:startIndex>1</openSearch:startIndex>
   <openSearch:itemsPerPage>10</openSearch:itemsPerPage>
   <openSearch:Query role="request" searchTerms="1234567890 " startPage="1" />
   <openSearch:Image height="1" width="1" type="image/gif">https://www.search.ch/audit/CP/tel/de/api</openSearch:Image>
   <entry>
      <id>urn:uuid:ef812a3ea1e8e6f0</id>
      <updated>2018-08-28T02:00:00Z</updated>
      <published>2018-08-28T02:00:00Z</published>
      <title type="text">Power, Max</title>
      <content type="text">Max Power, Firststreet 1 6000 Switzerland/SZ 123 456 78 90</content>
      <tel:nopromo>*</tel:nopromo>
      <author>
         <name>tel.search.ch</name>
      </author>
      <link href="https://tel.search.ch/switzerland/maxPower" title="Details" rel="alternate" type="text/html" />
      <link href="https://tel.search.ch/switzerland/maxPower" type="text/x-vcard" title="VCard Download" rel="alternate" />
      <link href="https://tel.search.ch/edit/?id=ef812a3ea1e8e6f0" rel="edit" type="text/html" />
   </entry>
</feed>

由local.ch(瑞士的电话簿)生成

通过这篇文章(InvalidOperationException deserializing Atom XML),我创建了这个微型控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace consXMLtest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer xs = new XmlSerializer(typeof(RssFeedModel));
            XmlTextReader reader = new XmlTextReader(@"D:/test.xml");
            RssFeedModel rssFeedModel = (RssFeedModel)xs.Deserialize(reader);

            Console.WriteLine(rssFeedModel.Title);
            Console.ReadKey();

        }
    }
}

[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class RssFeedModel
{
    [XmlElement("title")]
    public string Title { get; set; }

    [XmlElement("link")]
    public List<Link> link { get; set; }

    [XmlElement("entry")]
    public List<Entry> entry { get; set; }
}
[XmlRoot("link")]
public class Link
{
    [XmlAttribute("rel")]
    public string rel { get; set; }
    [XmlAttribute("type")]
    public string type { get; set; }
}
[XmlRoot("entry")]
public class Entry
{
    [XmlElement("id")]
    public string Id { get; set; }

    [XmlElement("published")]
    public DateTime PublishDate { get; set; }

    [XmlElement("content")]
    public Content content { get; set; }
}
[XmlRoot("content")]
public class Content
{
    [XmlAttribute("type")]
    public string type { get; set; }
    [XmlText]
    public string text { get; set; }
}

现在我的问题是:如何访问“内容”类? 对于我的应用程序,我只需要内容。

谢谢您的帮助。

来自瑞士的问候 niju

2 个答案:

答案 0 :(得分:0)

尝试一下:

public static void Main()
    {
        XmlSerializer xs = new XmlSerializer(typeof(RssFeedModel));
        XmlTextReader reader = new XmlTextReader(@"D:/test.xml");
        RssFeedModel rssFeedModel = (RssFeedModel)xs.Deserialize(reader);


        Console.WriteLine(rssFeedModel.entry.FirstOrDefault()?.content.text);
        Console.ReadKey();
    }

答案 1 :(得分:0)

如果我对您的理解正确,那么您只需使用

遍历所有条目即可。

foreach(var c in rssFeedModel.entry)            
    Console.WriteLine(c.content.text);

并可以读出content属性。