C#XMLNS复杂的反序列化

时间:2013-10-27 14:35:54

标签: c# xml xml-namespaces

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

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

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

[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®Legends)。 我需要在“entry”和“live:media”之后获取参数,我不知道如何在开头使用“live:”接收参数。

编辑:

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

它运作良好,但是:

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

此返回的null媒体类:/

1 个答案:

答案 0 :(得分:2)

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

例如,要正确映射itemNum属性,请在您的输入类中尝试:

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

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

class media
{
    public int mediaType{ get; set; }
    public string reducedDescription{ get; set; }
etc. etc.
}

然后,您需要将所有这些“实时”属性从您的入门级移动到新的媒体类中,为您留下可能如下所示的入口类:

[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中的各种属性对Xml进行一些重要的重新映射和转换,但是我发现更容易实现,理解和维护这种类型如果C#类将1:1映射到xml结构上,那么就可以了。