【问题标题】:Manually Create classes to map to XML Request Response手动创建类以映射到 XML 请求响应
【发布时间】:2012-06-19 13:01:08
【问题描述】:

我被指派为使用 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;
    }
}

有时,我觉得我承受了太多的痛苦。有时候,我觉得痛苦是值得的。 各位,你们怎么看? 另外,我的班级设计有什么改进吗?

【问题讨论】:

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

标签: c# .net xml xsd.exe


【解决方案1】:

您真的想太多了……您可以使用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);
        }
    }
}

最后一点,您会注意到我并没有真正费心对日期等的转换做任何过于花哨的事情,但是您可以轻松地对此进行扩展以获取更多详细信息。你应该从中得到的主要一点是,你真的在​​想这整件事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多