使用Namspace将XML转换为Linq

时间:2017-06-14 17:45:06

标签: c# xml linq

这是我试图解析的XML文件的一个例子(它是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>

如果我只想要XML的一个元素,那么这段代码就可以了:

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

问题是我想选择XML的多个元素。我尝试过使用p.descendents,p.Attribute(&#34; player_key&#34;),p.element(&#34; player_key&#34;)。他们都没有工作。这是我要去的一般方向(这不起作用)

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,
            });

我知道很接近,但是无法弄清楚。

0 个答案:

没有答案