tags:

views:

99

answers:

4

Hi Guys/gals

I have an xml doc similar to this:

<Root>

    <MainItem ID="1">
        <SubItem></SubItem>
        <SubItem></SubItem>
        <SubItem></SubItem>
    </MainItem>
    <MainItem ID="2">
        <SubItem></SubItem>
        <SubItem></SubItem>
        <SubItem></SubItem>
    </MainItem>

    ...
</Root>

I want to return the whole of the MainItem element based on the value of attribute ID. So effectively if Attribute ID is equal to 2, then give me that MainItem element back.

I can't work out how to do this with LINQ. There seems to be a load of information on google, but I just can't quite seem to find what I'm looking for.

Little help ?

TIA

:-)

+2  A: 

It could be something like this:

        XDocument doc = XDocument.Load("myxmlfile.xml");
        XElement mainElement = doc.Element("Root")
                                    .Elements("MainItem")
                                    .First(e => (int)e.Attribute("ID") == 2);
        // additional work
bruno conde
Just be aware that the call to `.First()` will throw an exception if there's no matching element. Use `.FirstOrDefault()` if you want to avoid the exception and get back a NULL value if nothing is found.
marc_s
Will do marc, thanks.
JustAPleb
I'd use `(int)e.Attribute("ID") == 2`.
AnthonyWJones
I prefer this to my effort, I don't like the select syntax for LINQ.
Chris S
@AnthonyWJones, I took your suggestion. Thanks.
bruno conde
A: 

From here: How to: Filter on an Attribute (XPath-LINQ to XML)

// LINQ to XML query
IEnumerable<XElement> list1 =
    from el in items.Descendants("MainItem")
    where (string)el.Attribute("ID") == "2"
    select el;

// XPath expression
IEnumerable<XElement> list2 = items.XPathSelectElements(".//MainItem[@ID='2']");
Rubens Farias
Thanks for the help! Much appreciated.
JustAPleb
-1 for XpathSelect(). That's not the linq way.
No Refunds No Returns
As you probably noted, I just cited a Microsoft page on subject
Rubens Farias
+2  A: 

How about this:

// load your XML
XDocument doc = XDocument.Load(@"D:\linq.xml");

// find element which has a ID=2 value
XElement mainItem = doc.Descendants("MainItem")
                          .Where(mi => mi.Attribute("ID").Value == "2")
                          .FirstOrDefault();

if(mainItem != null)
{ 
  // do whatever you need to do
}

Marc

marc_s
Thanks for the help! Much appreciated.
JustAPleb
+2  A: 

I changed your XML slightly to have values:

<?xml version="1.0"?>
<Root>
    <MainItem ID="1">
        <SubItem>value 1</SubItem>
        <SubItem>val 2</SubItem>
        <SubItem></SubItem>
    </MainItem>
    <MainItem ID="2">
        <SubItem></SubItem>
        <SubItem></SubItem>
        <SubItem></SubItem>
    </MainItem>
</Root>

And with this LINQ:

XDocument xmlDoc = XDocument.Load(@"C:\test.xml");
var result = from mainitem in xmlDoc.Descendants("MainItem")
             where mainitem.Attribute("ID").Value == "1"
             select mainitem;


foreach (var subitem in result.First().Descendants())
{
    Console.WriteLine(subitem.Value);
}

Console.Read();
Chris S
Nice, thanks. Me thinks I need to get a decent book on LINQ...something else to add to the list ;-)
JustAPleb