After my question on StackOverflow, I've written code that should create a Config
class from an XML file. XML loading is mixed with structures declaration and break in a lot of small pieces.
However, I'm not sure if this is bad or good. How would you refactor this? Probably some simple design pattern can be used, but again, I'm not sure that I should use design patterns for such simple things as XML loading.
using System; using System.Net; using System.Xml; namespace Fast.Config { public class Config { public Config(string configFile) { Load(configFile); } public Channel Fond { get; private set; } private void Load(string file) { XmlDocument doc = new XmlDocument(); doc.Load(file); XmlElement fondChannel = doc.SelectNodes("//channel[@id=\"FOND\"]")[0] as XmlElement; Fond = new Channel(fondChannel); } } public class Channel { public Channel(XmlElement channel) { Load(channel); } private void Load(XmlElement node) { var connections = node.SelectNodes(".//connections"); var ordersIncrementalConnection = (connections[0] as XmlElement).SelectNodes(".//connection[@id=\"OLR\"]"); OrdersIncremental = new Connection(ordersIncrementalConnection[0] as XmlElement); } public Connection OrdersIncremental { get; private set; } } public class Connection { public Connection(XmlElement connection) { Load(connection); } private void Load(XmlElement connection) { XmlElement feedA = connection.SelectNodes(".//feed[@id=\"A\"]")[0] as XmlElement; FeedA = new Feed(feedA); XmlElement feedB = connection.SelectNodes(".//feed[@id=\"B\"]")[0] as XmlElement; FeedB = new Feed(feedB); } public Feed FeedA { get; private set; } public Feed FeedB { get; private set; } } public class Feed { public Feed(XmlElement feed) { Load(feed); } private void Load(XmlElement feed) { } } }