C#:与命名空间CLR对象反序列化的XML [英] c#: Deserializing xml with namespaces to clr object

查看:98
本文介绍了C#:与命名空间CLR对象反序列化的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助与XmlSerializer的。我有以下XML片段:

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>

和我也有下面的C#对象:

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



一切完美的作品,到目前为止,除了物业的ReplyTo始终保持为空。我需要从

我会,如果有人告诉我,我错过了什么真的很开心src属性。谢谢!

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!

推荐答案

您所需要的命名空间是http://purl.org/syndication/thread/1.0\"

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

THR只是的别名的 - 由的xmlns声明:THR 上方

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

所以:

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

这篇关于C#:与命名空间CLR对象反序列化的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆