This question contains some C# code I've written as well as a XML format I have designed. My C# code runs a series of "jobs" by loading the XML file and then calling different SQL stored procedures and E-Mailing the results to different addresses. My question specifically relates to the C# code to parse the XML document and also how well designed the XML format is.
The code allows for a variable number of email addresses and stored procedure parameters. To allow for multiple named stored procedure parameters, I'm using a dictionary. The key is the name of the parameter and the value is the value to be used with said parameter.
My questions:
- How well designed is the XML?
- How good or bad is the C# code to parse the file?
- How can it be improved?
- Is there anything majorly wrong with it?
This is the XML format I'm using (in this case there are only two jobs defined):
<?xml version="1.0" encoding="utf-8"?>
<jobService>
<job name="Report" storedProcedure="Procedure">
<procedureParameter name="anotherStoredProcedureParameter1" value="test" />
<procedureParameter name="anotherStoredProcedureParameter2" value="123456789" />
<emailSettings subjectName="Subject" attachmentName="SomeFileName1.csv" senderAddress="[email protected]">
<recipients>
<address value="[email protected] />
<address value="[email protected] />
</recipients>
<recipientsBcc>
<address value="[email protected] />
<address value="[email protected] />
</recipientsBcc>
<recipientsCC>
<address value="[email protected] />
<address value="[email protected] />
</recipientsCC>
</emailSettings>
</job>
<job name="Report #2" storedProcedure="Procedure2">
<procedureParameter name="anotherStoredProcedureParameter1" value="test" />
<procedureParameter name="anotherStoredProcedureParameter2" value="test" />
<procedureParameter name="anotherStoredProcedureParameter3" value="test" />
<procedureParameter name="anotherStoredProcedureParameter4" value="test" />
<emailSettings subjectName="Subject" attachmentName="SomeFileName1.csv" senderAddress="[email protected]">
<recipients>
<address value="[email protected] />
<address value="[email protected] />
</recipients>
<recipientsBcc>
<address value="[email protected] />
<address value="[email protected] />
</recipientsBcc>
<recipientsCC>
<address value="[email protected] />
<address value="[email protected] />
</recipientsCC>
</emailSettings>
</job>
</jobService>
And this is the C# code to parse the XML:
public IEnumerable<JobConfiguration> GetJobConfigurations()
{
XDocument jobs = XDocument.Load(@"jobs.xml");
var query = jobs.Descendants("jobService").Descendants("job")
.Select(x => new JobConfiguration
{
Name = x.Attribute("name").Value,
StoredProcedure = x.Attribute("storedProcedure").Value,
ProcedureParameters = x.Elements("procedureParameter").ToDictionary(p => p.Attribute("name").Value, p => p.Attribute("value").Value),
EmailProperties = x.Elements("emailSettings").Select(y => new EmailProperties
{
SubjectName = y.Attribute("subjectName").Value,
AttachmentName = y.Attribute("attachmentName").Value,
SenderAddress = y.Attribute("senderAddress").Value,
RecipientAddresses = y.Elements("recipients").Elements("address").Select(z => z.Attribute("value").Value).ToList(),
RecipientAddressesBcc = y.Elements("recipientsBcc").Elements("address").Select(z => z.Attribute("value").Value).ToList(),
RecipientAddressesCC = y.Elements("recipientsCC").Elements("address").Select(z => z.Attribute("value").Value).ToList()
}).ToList()
}).ToList();
return query;
}
And these are the JobConfiguration and EMailProperties classes:
public class JobConfiguration
{
public string Name { get; set; }
public string StoredProcedure { get; set; }
public IDictionary<string, string> ProcedureParameters { get; set; }
public EMailProperties EMailProperties { get; set; }
}
public class EmailProperties
{
public string SenderAddress { get; set; }
public IList<string> Recipients { get; set; }
public IList<string> RecipientsBcc { get; set; }
public IList<string> RecipientsCC { get; set; }
}