2

我正在尝试反序列化 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; }

这返回空媒体类:/

4

1 回答 1

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 结构。

于 2013-10-27T15:00:27.013 回答