Reputation: 1103
Custom XML:
<BrowsableGroups>
<BrowsableGroup Name="CSS">
<Path Type="directory" Value="Content"/>
<Path Type="file" Value="Content/test.css"/>
</BrowsableGroup>
<BrowsableGroup Name="CSHTML">
<Path Type="directory" Value="Views/Home"/>
<Path Type="directory" Value="Views/Admin"/>
</BrowsableGroup>
</BrowsableGroups>
Code: This is the code I am using now which uses a seperate xml file using Linq to XML which works quite well. But the requirement is to have this section within Web.Config. I have tried that first for more than 4 days, but haven't got it done exactly.
public class BrowsableGroups
{
public List<BrowsableGroup> Groups { get; set; }
public static List<BrowsableGroup> GetAllBrowsablePaths()
{
XDocument document = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/settings.xml"));
var groups = document.Descendants("BrowsableGroup")
.Select(p => new BrowsableGroup()
{
Name = p.Attribute("Name").Value,
Paths = p.Elements("Path").Select(m => new Path() { Type = m.Attribute("Type").Value, Value = m.Attribute("Value").Value }).ToList()
}).ToList();
return groups;
}
}
public class BrowsableGroup
{
public string Name { get; set; }
public List<Path> Paths { get; set; }
}
public class Path
{
public string Type { get; set; }
public string Value { get; set; }
}
Any help much appreciated.
Upvotes: 1
Views: 995
Reputation: 5480
A custom config section is very easy to create. There's plenty of examples of how to do it on Google.
You'll want to create a ConfigurationSection class for the outer section, a ConfigurationElementCollection for your 2 collections & a ConfigurationElement class for each item.
Your config will look slightly different, as by default the collection uses <add/>
, <remove/>
& <clear/>
elements
Upvotes: 2