Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Xml: selecting elements if an attribute value equals a node value in an IEnumerable<XElement>

Tags:

c#

xml

asp.net

linq

I create an IEnumerable object that just contains the nodes I want from an xml file:

IEnumerable<XElement> rosters = XDocument.Load("roster.xml")
                                         .Elements("rosterlist")
                                         .Elements("roster")
                                         .Where(w => w.Element("division")
                                                      .Value
                                                      .Equals("SUPER AWESOME DIVISION"));

So it's a collection of these:

<rosterlist>
    <roster>
        <userid>1</userid>
        <name></name>
        <etc></etc>
    </roster>
    <roster>
        <userid>2</userid>
        <name></name>
        <etc></etc>
    </roster>
</rosterlist>

I want to grab only the users where the userid attribute is also a userid node within the rosters collection.

IEnumerable<XElement> users = XDocument.Load("user.xml")
                                       .Elements("userlist")
                                       .Elements("user")
                                       .Where(w => rosters.Elements("userid")
                                                          .Contains(w.Attribute("userid").Value));

But it is giving me an error:

The type arguments for method 'System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

What is wrong with my approach?

like image 327
lolcat Avatar asked Feb 22 '09 06:02

lolcat


1 Answers

One issue I see is that in the last code snippet, ...Elements("userid") returns a list of XElement objects which cannot contain a String returned by the Value property. This should work...

IEnumerable<XElement> rosters = obRoot.Elements("rosterlist").Elements("roster");
var rosterUserIds = (rosters.Elements("userid").Select(r => r.Value));
IEnumerable<XElement> users = obRoot.Elements("userlist").Elements("user")
                .Where(u => rosterUserIds.Contains(u.Attribute("userid").Value));

However I'd do this by using a join query.. Select user join rosters on userid
It'd go something like this

string sXml = @"
<root>
<rosterlist>
    <roster>
        <userid>1</userid>
        <name>R1</name>
        <etc></etc>
    </roster>
    <roster>
        <userid>2</userid>
        <name>R2</name>
        <etc></etc>
    </roster>
</rosterlist>
<userlist>
    <user userid='1'>
        <name>User on roster</name>
    </user>
    <user userid='5'>
        <name>User not on roster</name>
    </user>
</userlist>
</root>

";

XElement obRoot = XElement.Parse( sXml );
var results = from user in obRoot.Elements("userlist").Elements("user")
   join roster in obRoot.Elements("rosterlist").Elements("roster")
   on user.Attribute("userid").Value equals roster.Element("userid").Value
   select new {Name=user.Element("name").Value, RosterName=roster.Element("name").Value} ;

foreach (var v in results)
{
   Console.WriteLine("{0, -20} on Roster {1, -20}", v.Name, v.RosterName);
}

Outputs:

User on roster       on Roster R1
like image 90
Gishu Avatar answered Nov 05 '22 18:11

Gishu