Transform Entity with Linq to XML

Let's assume my example entity TeamPlayer looks like this:

PlayerId    PlayerName   Team       Position
----------------------------------------------
1           Nobody       Team A      Striker
1           Nobody       Team B      Center Midfield
1           Nobody       Team C      Substitute
2           Chuck Norris TeamA      ALL
2           Chuck Norris TeamB      Substitute

What i try to achieve is to write a C# method where i pass the player id. The result should be an XML string like this (if 1 is passed to the method):

<ns0:root xmlns:ns0="urn:teamsoftware:playermanager:playerovierview">
    <player>
        <playerid>1</playerid>
        <playername>Nobody</playername>
        <team>TeamA
TeamB
TeamC</team>
        <position>Striker
Center Midfield
Substiture</position>
    </player>
</ns0:root>

So if a player joins more teams they should be inside <team>tag, seperated by CR LF. I know this is an ugly solution, but data are delivered to a third-party tool so they have to look like this unfortunately :/

Any help is appreciated!

Jon Skeet
people
quotationmark

Ugly as that is, it doesn't sound too hard using string.Join to create the appropriate text contents. After fetching the relevant data - which I assume you know how to do - you could just use something like:

var records = ...; // The player records
XNamespace ns = "urn:teamsoftware:playermanager:playerovierview";
return new XElement(ns + "root",
    new XElement("player",
        new XElement("playerid", records.First().PlayerId),
        new XElement("playername", records.First().PlayerName),
        new XElement("team", string.Join("\r\n", records.Select(x => x.Team)),
        new XElement("position", string.Join("\r\n", records.Select(x => x.Position))
    )
);

people

See more on this question at Stackoverflow