c#: Deserializing xml with namespaces to clr object

送分小仙女□ 提交于 2019-12-31 03:52:05

问题


I need some help with XmlSerializer. I have to following xml fragment:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
  <id>tag:blogger.com,1999:blog-4233645339430781865.archive</id>
  <updated>2012-10-22T07:00:02.139+03:00</updated>
  <title type='text'>Code !t</title>      
  <link rel='alternate' type='text/html' href='http://www.etabakov.com/'/>
  <author>
    <name>Емил Табаков</name>
    <email>noreply@blogger.com</email>
    <gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//lh5.googleusercontent.com/-TbRwL19G85U/AAAAAAAAAAI/AAAAAAAAFxg/NRV6ZYqd9Wg/s512-c/photo.jpg'/>
  </author>
  <generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
  <entry>
    <id>tag:blogger.com,1999:blog-4233645339430781865.post-513753811167440871</id>
    <published>2012-10-12T11:22:35.759+03:00</published>
    <updated>2012-10-12T11:22:35.759+03:00</updated>
    <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/blogger/2008/kind#comment'/>
    <title type='text'>Great post indeed. I really like that you are prov...</title>
    <content type='html'>Great post indeed. I really like that you are providing information on .NET for freshers , Being enrolled  at http://www.wiziq.com/course/57-fresher-training-projects i found your information very helpful indeed. Thanks for it.</content>
    <link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4233645339430781865/8317071019326278340/comments/default/513753811167440871'/>        
    <author>
      <name>sarabjeet</name>
      <uri>http://www.blogger.com/profile/11223974173581186160</uri>
      <email>noreply@blogger.com</email>
      <gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/>
    </author>
    <thr:in-reply-to href='http://www.etabakov.com/2012/06/net-guy-velocityconf-2012-day-1.html' ref='tag:blogger.com,1999:blog-4233645339430781865.post-8317071019326278340' source='http://www.blogger.com/feeds/4233645339430781865/posts/default/8317071019326278340' type='text/html'/>
    <gd:extendedProperty name='blogger.itemClass' value='pid-899300522'/>        
  </entry>
</feed>

And I also have the following c# objects:

Feed.cs

[XmlRoot(ElementName = "feed", Namespace = "http://www.w3.org/2005/Atom"), XmlType("feed")]
public class Feed
{
    [XmlElement("id")]
    public string Id { get; set; }

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

    [XmlElement("author")]
    public Author Author { get; set; }

    [XmlElement("entry")]
    public List<Entry> Entry;
}
public class Entry
{
    [XmlElement("id")]
    public string Id { get; set; }

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

    [XmlElement("content")]
    public string Content { get; set; }

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

    [XmlElement("updated")]
    public DateTime Updated { get; set; }

    [XmlElement("category")]
    public List<Category> Categories;

    [XmlElement("author")]
    public Author Author { get; set; }

    [XmlElement(ElementName = "in-reply-to", Namespace = "thr", Type = typeof(ReplyTo), IsNullable = true)]
    public ReplyTo ReplyTo { get; set; }
}

public class ReplyTo
{
    [XmlAttribute("ref")]
    public string Id { get; set; }
}

Everything works perfectly so far, except that ReplyTo property always stays null. I need to get the src attribute from the

I will be really happy if someone show me what I'm missing. Thanks!


回答1:


The namespace you need is "http://purl.org/syndication/thread/1.0"

"thr" is just the alias - as declared by the xmlns:thr at the top.

So:

[XmlElement(ElementName = "in-reply-to", Namespace = "http://purl.org/syndication/thread/1.0", Type = typeof(ReplyTo), IsNullable = true)]
public ReplyTo ReplyTo { get; set; }


来源:https://stackoverflow.com/questions/13134329/c-deserializing-xml-with-namespaces-to-clr-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!