I have been assigned to implement an interface to an API that uses XML request/response. The API providers don't provide any xsd(s) for the XML calls.
I generated the C# classes using xsd.exe: .xml -> .xsd -> .cs However, I did not find the generated classes satisfactory, as the calls include a lot of lists, which xsd.exe doesn't handle properly.
Should I take the pain and create classes manually that maps to all the request/responses? That might help in maintaining the code easily later on. Or should I just use Xml classes given by .Net, and write methods to create the XML request/responses? That will take lesser time, but may become tough in the maintenance phase.
Here is a sample class that I have created for a corresponding XML element:
XML Element
<Product ID="41172" Description="2 Pers. With Breakfast" NonRefundable="YES" StartDate="2010-01-01" EndDate="2010-06-30" Rate="250.00" Minstay="1" />
The corresponding class
internal class ProductElement : IElement
{
private const string ElementName = "Product";
private const string IdAttribute = "ID";
private const string DescriptionAttribute = "Description";
private const string NonRefundableAttribute = "NonRefundable";
private const string StartDateAttribute = "StartDate";
private const string EndDateAttribute = "EndDate";
private const string RateAttribute = "Rate";
private const string MinStayAttribute = "Minstay";
private string Id { get; private set; }
internal string Description { get; private set; }
internal bool IsNonRefundable { get; private set; }
private DateRange _dateRange;
private string ParseFormat = "yyyy-MM-dd";
private decimal? _rate;
private int? _minStay;
internal ProductElement(string id, DateRange dateRange, decimal? rate, int? minStay)
{
this.Id = id;
this._dateRange = dateRange;
this._rate = rate;
this._minStay = minStay;
}
internal ProductElement(XElement element)
{
this.Id = element.Attribute(IdAttribute).Value;
this.Description = element.Attribute(DescriptionAttribute).Value;
this.IsNonRefundable = element.Attribute(NonRefundableAttribute).Value.IsEqual("yes") ? true : false;
}
public XElement ToXElement()
{
var element = new XElement(ElementName);
element.SetAttributeValue(IdAttribute, _id);
element.SetAttributeValue(StartDateAttribute, _dateRange.Start.ToString(ParseFormat, CultureInfo.InvariantCulture));
element.SetAttributeValue(EndDateAttribute, _dateRange.End.ToString(ParseFormat, CultureInfo.InvariantCulture));
element.SetAttributeValue(RateAttribute, decimal.Round(_rate.Value, 2).ToString());
element.SetAttributeValue(MinStayAttribute, _minStay.Value.ToString());
return element;
}
}
At times, I think I am taking too much pain. Sometimes, I think the pain is worth taking. What is your opinion, people? Also, any improvements in my class design?