Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
First Linq To XML query - having problems
Message
From
14/04/2009 17:56:26
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
 
To
14/04/2009 17:44:42
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
ASP.NET
Category:
LINQ
Miscellaneous
Thread ID:
01394795
Message ID:
01394866
Views:
52
>>>>Here's my query:
>>>>
>>>>            var query = from m in _AdministrativeTabMappings.Elements("Mappings").Elements("Mapping")
>>>>                        where m.Attribute("Page").Value == page
>>>>                        select m;
>>>>
>>>>
>>>>Query always returns a NullReferenceException. This works without the where clause. Page is a variable which I confirmed equals "bonds/administrator/AdministratorHome.aspx".
>>>
>>>Page isn't an attribute, it's an element. An attribute would be something like:
>>>
>>>
>>><Page name="MyNameAttribute"Value</Page
>>>
>>>
>>>I can't test this, but maybe something like this would work?
>>>
>>>
>>>var query = from m in _AdministrativeTabMappings.Elements("Mappings").Decendants("Page")
>>>                        where m.Value == page
>>>                        select m;
>>>
>>>
>>
>>That returns one result like I would expect, but this code fails when I'm trying to get the value I want:
>>
>>            foreach (var item in query)
>>            {
>>                result = item.Element("Tab").Value;
>>            }
>>
>
>
>In comprehension syntax:
>
>
string result = ( from m in _AdministrativeTabMappings.Elements("Mapping")
> where m.Element("Page").Value == page
> select m).Single().Element("Tab").Value;
>
>In method syntax:
>
>
>string result = 
>   _AdministrativeTabMappings.Elements("Mapping")
>   .Where( m => m.Element("Page").Value == page )
>   .Single()
>   .Element("Tab").Value;
>
Cetin

thanks!
Very fitting: http://xkcd.com/386/
Previous
Reply
Map
View

Click here to load this message in the networking platform