I am using the code below
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<widgets>
<widget>
<url>~/Portal/Widgets/ServicesList.ascx</url>
<castAs>ServicesWidget</castAs>
<urlType>ascx</urlType>
<parameters>
<PortalCategoryId>3</PortalCategoryId>
</parameters>
</widget>
<widget>
<url>www.omegacoder.com</url>
<castAs>ServicesWidget</castAs>
<urlType>htm</urlType>
<parameters>
<PortalCategoryId>41</PortalCategoryId>
</parameters>
</widget>
</widgets>";
XDocument loaded = XDocument.Parse( xml );
var widgets = from x in loaded.Descendants( "widget" )
select new
{
URL = x.Descendants( "url" ).First().Value,
Category = x.Descendants( "PortalCategoryId" ).First().Value
};
foreach ( var wd in widgets )
Console.WriteLine( "Widget at ({0}) has a category of {1}", wd.URL, wd.Category );
This gives me the URL and Category for the first widget only? Not sure how can I get the values for second one.
Also how can I get the index of the widget like 0 and 1 and so on….depending on how many widgets are there.
thanks
Your query return entries for both widgets for me, I’ve tried your code without any changes.
Following query will help you get index for each widget as well:
String representation: