C# 反序列化 Atom XML

C# Deserializing Atom XML

我想反序列化以下 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>

Generatet by local.ch(来自瑞士的电话簿)

我用这个 post () 创建了这个微型控制台应用程序:

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; }
}

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

感谢您的帮助。

来自瑞士的问候 你住

试试这个:

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();
    }

如果我理解正确的话,您只需使用

遍历所有条目
foreach(var c in rssFeedModel.entry)            
    Console.WriteLine(c.content.text);

并且可以读出 content 属性。