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

问题描述:

我已被分配为实现使用XML请求/响应的API的接口。 API提供程序不会为XML调用提供任何xsd(s)。手动创建要映射到XML请求响应的类

我生成使用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; 
    } 
} 

有时候,我觉得我走太痛苦了。有时候,我认为这种痛苦是值得的。 你的意见是什么? 此外,我的课堂设计有什么改进?

+0

我会专注于修复您的xsd,以准确反映您期望收到的xml并继续使用xsd.exe工具。如果您必须创建自己的类,那么您应该使用XML.Serialization命名空间和属性。 – Maess

您真的在想这个问题......您可以使用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 = "<Product ID=\"41172\" Description=\"2 Pers. With Breakfast\" NonRefundable=\"YES\" StartDate=\"2010-01-01\" EndDate=\"2010-06-30\" Rate=\"250.00\" Minstay=\"1\" />"; 
     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); 
     } 
    } 
} 

最后要注意,你会发现我真的不打扰做任何事情过于花哨的日期转换等等,但你可以很容易地扩展到这个额外的细节。你应该从这个问题中解脱出来的主要原因是你真的在想这件事。