0

This is an example of the XML file I am trying to parse (it is one record out of 25)...

<?xml version="1.0" encoding="UTF-8" ?> 
<fantasy_content xml:lang="en-US" yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/game/223/players"     xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" time="5489.1560077667ms" copyright="Data provided by Yahoo! and STATS, LLC" refresh_rate="60"     xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
    <game>
        <game_key>223</game_key> 
        <game_id>223</game_id> 
        <name>Football PLUS</name> 
        <code>pnfl</code> 
        <type>full</type> 
        <url>http://football.fantasysports.yahoo.com/f2</url> 
        <season>2009</season> 
        <players count="25">
            <player>
                <player_key>223.p.8261</player_key> 
                <player_id>8261</player_id> 
                <name>
                    <full>Adrian Peterson</full> 
                    <first>Adrian</first> 
                    <last>Peterson</last> 
                    <ascii_first>Adrian</ascii_first> 
                    <ascii_last>Peterson</ascii_last> 
                </name>
                <editorial_player_key>nfl.p.8261</editorial_player_key> 
                <editorial_team_key>nfl.t.16</editorial_team_key> 
                <editorial_team_full_name>Minnesota Vikings</editorial_team_full_name> 
                <editorial_team_abbr>Min</editorial_team_abbr> 
                <bye_weeks>
                    <week>9</week> 
                </bye_weeks>
                <uniform_number>28</uniform_number> 
                <display_position>RB</display_position> 
                <headshot>
                    <url>http://l.yimg.com/iu/api/res/1.2/7gLeB7TR77HalMeJv.iDVA--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/nfl/players_l/20120913/8261.jpg</url> 
                    <size>small</size> 
                </headshot>
                <image_url>http://l.yimg.com/iu/api/res/1.2/7gLeB7TR77HalMeJv.iDVA--/YXBwaWQ9eXZpZGVvO2NoPTg2MDtjcj0xO2N3PTY1OTtkeD0xO2R5PTE7Zmk9dWxjcm9wO2g9NjA7cT0xMDA7dz00Ng--/http://l.yimg.com/j/assets/i/us/sp/v/nfl/players_l/20120913/8261.jpg</image_url> 
                <is_undroppable>1</is_undroppable> 
                <position_type>O</position_type> 
                <eligible_positions>
                    <position>RB</position> 
                </eligible_positions>
                <has_player_notes>1</has_player_notes> 
            </player>
            <player>
        </players>
    </game>
</fantasy_content>

This code works if I only want ONE element of the XML:

var players = res.Descendants(ns + "player_key").Select(p => new
        {
            player_key = p.Value
        });

The problem is i want to select multiple elements of the XML. I have tried using p.descendents, p.Attribute("player_key"), p.element("player_key"). none of them work. Here is the general direction i am going (that does not work)

var players = res.Descendants(ns + "player")
                .Select(p => new
            {
                player_key = p.Element("player_key").Value,
                player_key = p.Element("player_key").Value,
                player_id = p.Element("player_id").Value,
                name = p.Element("full").Value,
                posi = p.Element("display_position").Value,
                jerseyNum = p.Element("uniform_number").Value,
            });

I know am close, but just can't figure it out.

maccettura
  • 10,514
  • 3
  • 28
  • 35
dave317
  • 754
  • 2
  • 12
  • 30
  • 1
    If you post XML with hyphens on the left margin, it can't be parsed. Anybody who wants to test your code has to remove them. This is a hassle. It doesn't help people to help you. `p.Attribute("player_key")` was good though. – 15ee8f99-57ff-4f92-890c-b56153 Jun 14 '17 at 17:46
  • Yes, it was very similar to that other question. Not the exact same issue but close enough that I was able to fix it. – dave317 Jun 14 '17 at 18:02

0 Answers0