c# - 我将如何为下面的代码生成 Xml ... Order 类中 void getxml() 方法的代码,这取决于 Delivery & Recipient 类

标签 c#

public class OrderXml
{

 public enum DeliveryType { FTP, Email, HDD, Tape, Aspera, MLT };
    public class Order
    {
        public int UserId { get; private set; }
        public int OrderBinId { get; private set; }
        public int TenantId { get; private set; }
        public Delivery Delivery { get; set; }
        public Recipient Recipient { get; private set; }
        public string[] AssetDmGuids { get; set; }
        public Order(int orderBinId, string[] assetDmGuids, DeliveryType type, int recipientId, string recipientName)
        {
            Delivery = new Delivery(type);
            Recipient = new Recipient(recipientId, recipientName);
            // UserId = SessionHelper.Instance.GetUserId();
            // TenantId = SessionHelper.Instance.GetTenantID().ToString();
            OrderBinId = orderBinId;
            AssetDmGuids = assetDmGuids;
        }
        public void GetXml()
        {
        }
    }
    public class Recipient
    {
        int _id;
        string _name = string.Empty;
        public Recipient(int id, string name)
        {
            this._id = id;
            this._name = name;
        }
        public int Id { get { return _id; } }
        public string Name { get { return _name; } }
    }
    public class Delivery
    {
        DeliveryType _deliveryType;
        string _ftpLocation = string.Empty;
        string _ftpPath = string.Empty;
        string[] _emailAddresses;
        string _deliveryAddress = string.Empty;
        string _deliveryComments = string.Empty;
        string _asperaLocation = string.Empty;
        public string FtpPath
        {
            get { return _ftpPath; }
            set { _ftpPath = value; }
        }
        public string[] EmailAddresses
        {
            get { return _emailAddresses; }
            set { _emailAddresses = value; }
        }
        public string DeliveryAddress
        {
            get { return _deliveryAddress; }
            set { _deliveryAddress = value; }
        }
        public string DeliveryComments
        {
            get { return _deliveryComments; }
            set { _deliveryComments = value; }
        }
        public string AsperaLocation
        {
            get { return _asperaLocation; }
            set { _asperaLocation = value; }
        }
        public string FtpLocation
        {
            get { return _ftpLocation; }
            set { _ftpLocation = value; }
        }
        public Delivery(DeliveryType type)
        {
            _deliveryType = type;
        }

    }
    public static void Main()
    {
        Order ord = new Order(1, new string[] { "123", "124", "125" }, DeliveryType.Tape, 1, "vh1");

}

}

最佳答案

public XmlDocument GetXml()
{
        XmlDocument retValue = new XmlDocument();
        try
        {
                XmlSerializer xs = new XmlSerializer(this.GetType()); 
            Stream stream = new MemoryStream();
            xs.Serialize( stream, toSerialize );
            stream.Position = 0;
            retValue.Load( stream );
        }
        catch (Exception ex)
        {
        }
    return retValue;
}

关于c# - 我将如何为下面的代码生成 Xml ... Order 类中 void getxml() 方法的代码,这取决于 Delivery & Recipient 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804566/

相关文章:

c# - 从供应商的 Global.asax 继承 Global.asax 时,Application_Start 不会触发

c# - 使用 Assert 语句时有什么方法可以忽略 Possible Null Reference Exception 警告?

c# AForge.NET - 如何将视频从相机保存到文件

c# - 从 C# 运行 .bat 文件

c# - 多次使用 "from"是否等同于加入?

c# - Bgr32 PixelFormat 中的第四个 channel 是什么

c# - 无法从 docker 容器连接到 SQL Server

javascript - MVC 中如何在两个模型之间共享对象?

c# - 下一个凌晨 3 点发生的日期时间

c# - 使用var有副作用吗?