手动创建类映射到XML请求响应 [英] Manually Create classes to map to XML Request Response

查看:160
本文介绍了手动创建类映射到XML请求响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已分配到实现接口到使用XML请求/响应的API。 该API提供者不为XML调用提供任何XSD(S)。

I have been assigned to implement an interface to an API that uses XML request/response. The API providers don't provide any xsd(s) for the XML calls.

我用XSD.EXE生成的C#类 但是,我没有找到生成的类令人满意的,因为调用包括很多表,这XSD.EXE不妥善处理。

I generated the C# classes using xsd.exe: .xml -> .xsd -> .cs However, I did not find the generated classes satisfactory, as the calls include a lot of lists, which xsd.exe doesn't handle properly.

我应该采取的痛苦和手动创建类映射到所有的请求/响应?这可能有助于维持code以后轻松。 或者我应该只是使用由净给定的XML类和写入方法创建XML请求/响应?这将需要较少的时间,但可能会在艰难维持阶段。

Should I take the pain and create classes manually that maps to all the request/responses? That might help in maintaining the code easily later on. Or should I just use Xml classes given by .Net, and write methods to create the XML request/responses? That will take lesser time, but may become tough in the maintenance phase.

下面是我的一个相应的XML元素创建了一个示例类:

Here is a sample class that I have created for a corresponding XML element:

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

有时候,我觉得我服用了太多的痛苦。有时候,我觉得痛苦是值得的。 对此你有何看法,人呢? 此外,在我的课堂设计任何改进?

At times, I think I am taking too much pain. Sometimes, I think the pain is worth taking. What is your opinion, people? Also, any improvements in my class design?

推荐答案

你真的在思考这个问题...您可以使用的System.Xml.Serialization 命名空间,真正为您节省时间和完成大部分的工作给你。

You're really over thinking the problem... you can use the System.Xml.Serialization namespace to really save you time and do most of the work for you.

使用这个代替:

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

而code测试:

And the code to test:

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

最后一个音符,你会发现我真的不打扰做任何事情过于花哨的日期,这样的转换,但是你可以很容易地在此展开的更多详细信息。你应该从这个拿走最主要的是,你真的很过分想着这件事。

One final note, you will notice I didn't really bother doing anything overly fancy with the conversion for dates and such, but you can easily expand upon this for the additional details. The main thing you should take away from this is that you're really over-thinking this whole thing.

这篇关于手动创建类映射到XML请求响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆