WP7如何解析XML?

时间:2011-10-07 05:55:23

标签: xml windows-phone-7

在wp7中,我想解析xml标签

.Xml:

<top>
<value name="Group A">
<team position="1" name="india" won="10" lose="5"/>
<team position="2" name="pakistan" won="5" lose="5"/>
</value>

<value name="Group B">
<team position="1" name="Aus" won="10" lose="5"/>
<team position="2" name="newzeland" won="5" lose="5"/>
</value>
</top>

我想要这样的输出,

Group A

1  India 10  5
2  pak    5  10

Group B

1  Aus        5    5
2  Neszeland  5   5

我正在使用像这样的解析器,

 list = (from story in xmlTweets.Descendants("value")

                             select new ViewModel
                             {
                                 group= story.Attribute("name").Value,

                             }).ToList();

list1 = (from story in xmlTweets.Descendants("team")

                             select new ViewModel
                             {
                                 position= story.Attribute("position").Value,
name= story.Attribute("name").Value,
won= story.Attribute("won").Value,
lose= story.Attribute("lose").Value,

                             }).ToList();

输出:

Group A

Group B

1  India 10  5
2  pak    5  10

1  Aus        5    5
2  Neszeland  5   5

请告诉我一些想法。

感谢。

1 个答案:

答案 0 :(得分:3)

目前,您有两个单独的列表 - 一个用于组,一个用于团队。在我看来,你的视图模型需要更丰富 - 比如:

list = xml.Descendants("value")
          .Select(group => new GroupViewModel
          {
              Group = (string) group.Attribute("name"),
              Results = group.Elements("team")
                             .Select(team => new TeamViewModel
                             {
                                 Position = (int) team.Attribute("position"),
                                 Name = (string) team.Attribute("name"),
                                 Won = (int) team.Attribute("won"),
                                 Lost = (int) team.Attribute("lose")
                             })
                             .ToList()
          })
          .ToList();