0

I have a program that extends a base class like so:

namespace ConsoleExample
{
    [Serializable, XmlRoot("ServiceConfig")]
    public class ServiceConfig : BaseConfig<ServiceConfig>
    {
    }
}

where BaseConfig includes the following method:

        public abstract class BaseConfig<T> where T : BaseConfig<T>
        {
    ... 
            public void LoadConfig()
            {
                // Load configuration file
                ConfigReader<BaseConfig<T>> _configReader = new ConfigReader<BaseConfig<T>>(configFileName);

                _configReader.Load();
...

and ConfigReader is:

    public class ConfigReader<T>
    {
...    
               public Boolean Load()
                {
                    try
                    {
                            reader = new XmlTextReader(new StreamReader(_actualPath));
                            XmlSerializer serializer = new XmlSerializer(typeof(T));
                            deserializedObject = (T)(serializer.Deserialize(reader));
                         }
                    }
        ...

and my XML is like so:

<?xml version="1.0" encoding="utf-8"?>

<ServiceConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <WindowTitle>Utility</WindowTitle>
  <LicenseKey />
  <BaseUri />

  <!-- Database connection information -->
  <DatabaseInfo>
    <ServerName>ABC</ServerName>
    <DatabaseName>XYZ</DatabaseName>
...

When I run this program I get the error message of "<ServiceConfig xmlns=''> was not expected error

I found this post that suggested adding the XMLRoot tag but that doesn't help. I think it might be because the deserialization is coded in the BaseConfig program and somehow not interpreting the XMLRoot correctly but am not sure how to correct.

EDIT:

Solved this by creating ConfigReader object using just (not BaseConfig) like so:

public void LoadConfig()
                {
                    // Load configuration file
                    ConfigReader<T> _configReader = new ConfigReader<T>(configFileName);
...
Community
  • 1
  • 1
BrianKE
  • 4,035
  • 13
  • 65
  • 115
  • Why are you reinventing the wheel? Why not just add a custom configuration section to app.config and let `ConfigurationManager` do all of the work? – D Stanley May 22 '14 at 13:01
  • @DStanley Can you explain further? Not sure what I am reinventing. – BrianKE May 22 '14 at 13:04
  • In case anyone comes across this post I solved the issue by creating the ConfigReader using just (See edit). – BrianKE May 22 '14 at 13:34

0 Answers0