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);
...