手动创建要映射到XML请求响应的类

我被指派实现一个使用XML请求/响应的API的接口。 API提供程序不为XML调用提供任何xsd(s)。

我使用xsd.exe生成了C#类:.xml – > .xsd – > .cs但是,我没有发现生成的类令人满意,因为调用包含很多列表,xsd.exe无法正常处理。

我是否应该手动创建并映射到所有请求/响应的类? 这可能有助于以后轻松维护代码。 或者我应该只使用.Net提供的Xml类,并编写方法来创建XML请求/响应? 这将花费更少的时间,但在维护阶段可能会变得艰难。

这是我为相应的XML元素创建的示例类:

XML元素

 

相应的课程

 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; } } 

有时候,我觉得我太痛苦了。 有时,我认为痛苦是值得的。 你有什么看法,人? 还有,我class级设计的任何改进?

您真的在想这个问题……您可以使用System.Xml.Serialization命名空间来真正节省您的时间并为您完成大部分工作。

改为使用它:

 public class Product { [XmlAttribute()] public long Id { get; set; } [XmlAttribute()] public string Description { get; set; } [XmlAttribute()] public string NonRefundable { get; set; } [XmlAttribute()] public string StartDate { get; set; } [XmlAttribute()] public string EndDate { get; set; } [XmlAttribute()] public decimal Rate { get; set; } [XmlAttribute()] public bool Minstay { get; set; } } 

以及要测试的代码:

 class Program { static void Main(string[] args) { string xml = ""; XmlSerializer ser = new XmlSerializer(typeof(Product)); using(MemoryStream memStream = new MemoryStream()) { byte[] data = Encoding.Default.GetBytes(xml); memStream.Write(data, 0, data.Length); memStream.Position = 0; Product item = ser.Deserialize(memStream) as Product; Console.WriteLine(item.Description); } } } 

最后一点,你会注意到我并没有真正费心去做任何过于花哨的转换日期等等,但你可以轻松扩展这个以获取更多细节。 你应该从中得到的主要是你真的过分思考这一切。