首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# XMLNS复杂的反序列化

C# XMLNS复杂的反序列化
EN

Stack Overflow用户
提问于 2013-10-27 14:35:54
回答 1查看 607关注 0票数 2

我正在尝试反序列化XMLNS文件。

代码语言:javascript
运行
复制
  <feed xmlns:live="http://www.live.com/marketplace" xmlns="http://www.w3.org/2005/Atom">
  <live:totalItems>1177</live:totalItems>
  <live:numItems>1</live:numItems>
  <title>FindGames Results</title>
  <updated>2013-09-19T09:28:02.77Z</updated>
  <entry live:itemNum="34" live:detailView="3">
    <id>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</id>
    <updated>2013-09-19T09:28:02.77Z</updated>
    <title>Rayman® Legends</title>
    <content type="text">game content</content>
    <live:media>
      <live:mediaType>1</live:mediaType>
      <live:gameTitleMediaId>urn:uuid:66ACD000-77FE-1000-9115-D802555308C3</live:gameTitleMediaId>
      <live:reducedTitle>Rayman® Legends</live:reducedTitle>
      <live:reducedDescription>The Glade of Dreams is in trouble once again! During a 100-year nap, nightmares have multiplied and spread, creating new monsters even scarier than before!</live:reducedDescription>
      <live:availabilityDate>2013-06-11T00:00:00</live:availabilityDate>
      <live:releaseDate>2013-08-29T00:00:00</live:releaseDate>
      <live:ratingId>20</live:ratingId>
      <live:developer>Ubisoft</live:developer>
      <live:publisher>Ubisoft</live:publisher>
      <live:newestOfferStartDate>2013-09-13T09:00:00Z</live:newestOfferStartDate>
      <live:totalOfferCount>1</live:totalOfferCount>
      <live:titleId>1431505091</live:titleId>
      <live:effectiveTitleId>1431505091</live:effectiveTitleId>
      <live:gameReducedTitle>Rayman® Legends</live:gameReducedTitle>
      <live:ratingAggregate>4.50</live:ratingAggregate>
      <live:numberOfRatings>1193</live:numberOfRatings>
    </live:media>
  </entry>
</feed>

我的当前代码:反序列化类:

代码语言:javascript
运行
复制
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
    public string title { get; set; }
    public string totalItems { get; set; }
    public string reducedDescription{ get; set; }
    public string ratingId { get; set; }
    public string developer { get; set; }
    public string publisher { get; set; }
    public string tittleId { get; set; }
    public string ratingAggregate { get; set; }
    public string numberOfRatings { get; set; }
    public string boxImage { get; set; }
    public string categories { get; set; }
}

class deserializeXML
{
    public static void deserialize()
    {
        using (StreamReader reader = new StreamReader("Query.xml"))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(entry));
            entry x1 = serializer.Deserialize(reader) as entry;
        }
    }
}

我刚收到标题(FindGames结果而不是Rayman传奇)。我需要获得参数后的“输入”和“实时:媒体”,我不知道如何接收参数与“现场:”在开始。

编辑:

代码语言:javascript
运行
复制
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int numItems { get; set; }
[XmlElement(Namespace = "http://www.live.com/marketplace")]
public int totalItems { get; set; }

效果很好,但是:

代码语言:javascript
运行
复制
   [XmlElement("media" Namespace="http://www.live.com/marketplace")]
   public media media{ get; set; }

返回空媒体类:/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-27 15:00:27

您需要显式地将那些不在atom命名空间中的类型和属性映射到它们各自的命名空间。XmlRoot中的命名空间声明只是指定根元素的命名空间。如果文档中有多个名称空间,则需要显式地调用它们。

例如,要正确映射itemNum属性,在entry类中尝试这样做:

代码语言:javascript
运行
复制
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class entry
{
    [XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
    public int itemNum { get; set; }

    public string title { get; set; }
    public string totalItems { get; set; }
    public string reducedDescription { get; set; }
    public string ratingId { get; set; }
    public string developer { get; set; }
    public string publisher { get; set; }
    public string tittleId { get; set; }
    public string ratingAggregate { get; set; }
    public string numberOfRatings { get; set; }
    public string boxImage { get; set; }
    public string categories { get; set; }
}

然后,您需要声明其他类型,以映射到媒体节点之类的内容中。

代码语言:javascript
运行
复制
class media
{
    public int mediaType{ get; set; }
    public string reducedDescription{ get; set; }
etc. etc.
}

然后,您需要将所有这些“活动”属性从条目类移到新媒体类中,留下一个条目类,如下所示:

代码语言:javascript
运行
复制
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
    public class entry
    {
        [XmlAttribute("itemNum", Namespace = "http://www.live.com/marketplace")]
        public int itemNum { get; set; }

        public string title { get; set; }

        [XmlElement("media" Namespace="http://www.live.com/marketplace")]
        public media media{ get; set; }
    }

您的类层次结构不需要与xml层次结构完全匹配,您可以使用System.Xml.Serialization中的各种属性对C#进行一些重要的重新映射和转换,但是,如果C#类将1:1映射到Xml结构上,那么实现、理解和维护这类东西就更容易了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19619232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档