我被指派实现一个使用 XML 请求/响应的 API 接口。
API 提供商不为 XML 调用提供任何 xsd。
我使用 xsd.exe 生成了 C# 类:.xml -> .xsd -> .cs
但是,我发现生成的类并不令人满意,因为调用包含大量列表,而 xsd.exe 无法正确处理这些列表。
我应该忍受痛苦并手动创建映射到所有请求/响应的类吗?这可能有助于以后轻松维护代码。
或者我应该只使用.Net 提供的 Xml 类,并编写方法来创建 XML 请求/响应?这将花费更少的时间,但在维护阶段可能会变得困难。
下面是我为相应的 XML 元素创建的示例类:
XML元素
<Product ID="41172" Description="2 Pers. With Breakfast" NonRefundable="YES" StartDate="2010-01-01" EndDate="2010-06-30" Rate="250.00" Minstay="1" />
对应班级
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;
}
}
有时,我觉得我承受了太多的痛苦。有时候,我觉得痛苦是值得的。
大家对此有何看法?
另外,我的班级设计有什么改进吗?