嵌套LINQ to XML检索

92vpleto  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(126)

I have an XML file with the following structure...

<Tournament TeamPlayers="1">
    <Teams>
        <Team>
            <TeamID></TeamID>
            <Name></Name>
            <Status></Status>
            <Memo></Memo>
            <ByeRounds></ByeRounds>
            <Players>
                <Player Seat="A">
                    <MemberNo></MemberNo>
                    <MemberName></MemberName>
                    <PlayerFirstName></PlayerFirstName>
                    <PlayerLastName></PlayerLastName>
                </Player>
            </Players>
        </Team>
    </Teams>
    <Staff>
        <Player>
            <MembershipNo></MembershipNo>
            <PlayerFirstName></PlayerFirstName>
            <PlayerLastName></PlayerLastName>
            <MembershipName></MembershipName>
            <Position></Position>
        </Player>
    </Staff>
    
    <Penalties>
        <Penalty>
            <Judge></Judge>
            <Player></Player>
            <InfractionType></InfractionType>
            <PenaltyType></PenaltyType>
            <Notes></Notes>
        </Penalty>
    </Penalties>
</Tournament>

The idea is that I would iterate through each child node ( Teams , Staff , Penalty ) and assign each into a list. I have the following bit of code to start but can't figure out how to go into the nested Players element and assign it to .tpPlayerA as part of the New PlayerInfo construct.

Dim TeamsNodes = xDoc.Descendants("Teams")

        lstOutputTeams = team.Descendants("Team").[Select](Function(t) New TournamentTeam With {
                                              .intTeamID = t.Element("TeamID"),
                                              .intByeRounds = t.Element("ByeRounds"),
                                              .strTeamName = t.Element("Name"),
                                              .strMemo = t.Element("Memo"),
                                              .intStatus = t.Element("Status"),
                                              .tpPlayerA = (From p In team.Descendants("Players") Select New PlayerInfo With {
                                                        .strMembershipNo = p.Element("MemberNo").Value,
                                                        .strPlayerFirstName = p.Element("PlayerFirstName").Value,
                                                        .strPlayerLastName = p.Element("PlayerLastName").Value,
                                                        .strMembershipName = p.Element("MemberName")})
                                            }).ToList()
    Next

    lstFinalOutput.Add(lstOutputTeams)

(The above kind of ported from this answer: Nesting LINQ to XML )
However running the above,I get the error "Input string was not in a correct format". What did I get wrong here?

bq9c1y66

bq9c1y661#

我想我错过了什么...但是,

Dim xe As XElement
    'to load from file
    'xe = XElement.Load("path")

    ' testing use literal
    xe = <Tournament TeamPlayers="1">
             <Teams>
                 <Team>
                     <TeamID></TeamID>
                     <Name></Name>
                     <Status></Status>
                     <Memo></Memo>
                     <ByeRounds></ByeRounds>
                     <Players>
                         <Player Seat="A">
                             <MemberNo></MemberNo>
                             <MemberName></MemberName>
                             <PlayerFirstName></PlayerFirstName>
                             <PlayerLastName></PlayerLastName>
                         </Player>
                     </Players>
                 </Team>
             </Teams>
             <Penalties>
                 <Penalty>
                     <Judge></Judge>
                     <Player></Player>
                     <InfractionType></InfractionType>
                     <PenaltyType></PenaltyType>
                     <Notes></Notes>
                 </Penalty>
             </Penalties>
             <Staff>
                 <Player>
                     <MembershipNo></MembershipNo>
                     <PlayerFirstName></PlayerFirstName>
                     <PlayerLastName></PlayerLastName>
                     <MembershipName></MembershipName>
                     <Position></Position>
                 </Player>
             </Staff>
         </Tournament>

    Dim staff As List(Of XElement)
    Dim teams As List(Of XElement)
    Dim penalties As List(Of XElement)

    staff = (From el In xe.<Staff>.<Player>
              Select el).ToList

    teams = (From el In xe.<Teams>.<Team>
              Select el).ToList

    penalties = (From el In xe.<Penalties>.<Penalty>
                  Select el).ToList

相关问题