WP7 How to parse the XML?

对着背影说爱祢 提交于 2020-01-06 14:16:11

问题


In wp7 i want to parse xml tag

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

i want output like this,

Group A

1  India 10  5
2  pak    5  10

Group B

1  Aus        5    5
2  Neszeland  5   5

I' m using parser like this,

 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();

output:

Group A

Group B

1  India 10  5
2  pak    5  10

1  Aus        5    5
2  Neszeland  5   5

Please tell me some idea to do this.

thanks.


回答1:


Currently you've got two separate lists - one for groups and one for teams. It looks to me like your view model needs to be richer - something like:

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();


来源:https://stackoverflow.com/questions/7683527/wp7-how-to-parse-the-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!