2012-06-19 43 views
4

我已被分配爲實現使用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

回答

3

您真的在想這個問題......您可以使用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); 
     } 
    } 
} 

最後要注意,你會發現我真的不打擾做任何事情過於花哨的日期轉換等等,但你可以很容易地擴展到這個額外的細節。你應該從這個問題中解脫出來的主要原因是你真的在想這件事。