I have this XML markup:
<preferences>
<section name="PREF_SECTION_NAME_1">
<preference name="PREF_EXAMPLE_1" type="text">
<default value="false"></default>
</preference>
<preference name="PREF_EXAMPLE_2" type="radio">
<default value="true"></default>
</preference>
<preference name="PREF_EXAMPLE_3" type="radio">
<default value="false"></default>
</preference>
</section>
<section name="PREF_SECTION_NAME_2">
<preference name="PREF_EXAMPLE_4" type="radio">
<default value="true"></default>
</preference>
<preference name="PREF_EXAMPLE_5" type="radio">
<default value="false"></default>
</preference>
<preference name="PREF_EXAMPLE_6" type="radio">
<default value="true"></default>
</preference>
</section>
</preferences>
I want to get the default value for when the Preference attribute name = "PREF_EXAMPLE_2"
I got this:
var t = from p in xDocUser.Descendants("preference").Attributes("name").Where(d=>d.Value==preferencename) select p;
which gives me the Attributes for preference but not its child.
How can i do this please?
Well yes, you're asking for the attributes of preference. It sounds like you actually want something like:
var defaults = from pref in xDocUser.Descendants("preference")
where (string) pref.Attribute("name") == preferenceName
select (bool) pref.Element("default").Attribute("value");
That will give you an IEnumerable<bool>
of the default values for all the matching preferences.
See more on this question at Stackoverflow