1

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.

2
  • 1
    Have you considered using a custom ConfigurationSection for this? Commented Mar 8, 2014 at 14:46
  • Thanks Peter, I got this fixed myself. Needed a bit of research as am so new to this one. Will post my solution as well as I can't find exact example anywhere in Google which uses 3 levels of nested xml tag. :) Commented Mar 14, 2014 at 15:32

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.