gpt4 book ai didi

c# - 手动创建类以映射到 XML 请求响应

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:02 24 4
gpt4 key购买 nike

我被指派为使用 XML 请求/响应的 API 实现一个接口(interface)。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;
}
}

有时候,我觉得我太痛苦了。有时候,我觉得痛苦是值得的。大家有什么看法?另外,我的类(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 = "<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);
}
}
}

最后一点,您会注意到我并没有费心做任何关于日期等转换的过分花哨的事情,但是您可以轻松地扩展它以获取更多详细信息。您应该从中得出的主要结论是,您真的对整件事想得太多了。

关于c# - 手动创建类以映射到 XML 请求响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11101519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com