I've seen several SO posts regarding this same type of question but I cannot seem to get this to work.
I've got this XML file (in its entirety):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="StationsSection" type="AcmeTV_EcFtpClient.StationConfigurationSection, AcmeTV_EcFtpClient"/>
</configSections>
<StationsSection>
<Stations>
<add Comment="My comment goes here"
DestinationFolderPath="C:\TestInstallation"
FtpHostname="ftp://upload.servername.com/"
FtpFolderPath="myFolderPath/"
FtpUsername="myUserName"
FtpPassword="myFtpPassword"
FtpTimeoutInSeconds="20" />
</Stations>
</StationsSection>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
<appSettings>
<add key="NameOfService" value="AcmeECClient"/>
<add key="PollingFrequencyInSeconds" value="60"/>
</appSettings>
</configuration>
Here's the code for StationConfiguration:
public class StationConfiguration
{
readonly Regex OnlyAlphaNumericWithNoSpaces = new Regex("^[a-zA-Z0-9]*$");
public StationConfiguration() { }
public StationConfiguration(string comment, string ftpUsername, string ftpPassword, string destinationFolderPath)
{
Comment = comment;
FtpUsername = ftpUsername;
FtpPassword = ftpPassword;
DestinationFolderPath = destinationFolderPath;
}
public bool IsValidStation()
{
return OnlyAlphaNumericWithNoSpaces.IsMatch(Comment);
}
public bool IsValidUsername()
{
return OnlyAlphaNumericWithNoSpaces.IsMatch(FtpUsername);
}
public bool IsValidPassword()
{
return FtpPassword.Contains(' ') == false;
}
public bool IsValidFolderPath()
{
return Directory.Exists(DestinationFolderPath);
}
private string _comment;
public string Comment
{
get
{
return _comment;
}
set
{
_comment = value.ToUpper();
}
}
public string FtpUsername { get; set; }
public string FtpPassword { get; set; }
public string DestinationFolderPath { get; set; }
}
Here's my C# code that's attempting the parsing:
const string hardCodedConfigFilePath = @"C:\Program Files (x86)\MyApp.exe.config";
string xmlDocumentText = File.ReadAllText(hardCodedConfigFilePath);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDocumentText);
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement["StationsSection"]);
string firstStationConfiguration = doc.DocumentElement["StationsSection"].ChildNodes[0].InnerXml; //here's the chunk that contains my data
XmlSerializer ser = new XmlSerializer(typeof(StationConfiguration));
object obj = ser.Deserialize(reader);
The string called firstStationConfiguration contains this:
<add Comment="My comment goes here"
DestinationFolderPath="C:\TestInstallation"
FtpHostname="ftp://upload.servername.com/"
FtpFolderPath="myFolderPath/"
FtpUsername="myUsername"
FtpPassword="abcdefg" FtpTimeoutInSeconds="20" />
When the last C# line is executed, this is thrown:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in the XML document.
Please... How can I transform the Stations node (which may contain multiples) into C# objects?
StationConfigurationclass?StationConfigurationis. But using (StringReader stringReader = new StringReader(firstStationConfiguration)) { StationConfiguration sc = (StationConfiguration)serializer.Deserialize(stringReader); might go before your last line.