简体   繁体   English

C# 从 class ZA8CFDE6331BD59EB2AC96F8911C4B6666Z 创建 xml 文件

[英]C# creating xml file from a class object

My company has 17 payment types.我公司有 17 种付款方式。 When a new user is on-boarded all these 17 types should be assigned by default and a xml should be generated as below当新用户加入时,默认情况下应分配所有这 17 种类型,并应生成 xml,如下所示

XML representation of data: XML 数据表示:

<User>
    <UserId>nnn</UserId>
    <UserName>cccc</UserName>
    <PaymentTypes>
        <Payment>
          <PaymentType>Payment_Type1</PaymentType>
          <PaymentTypeID>1</PaymentTypeID>
          <Setting>true</Setting>
        </Payment>
        <Payment>
          <PaymentType>Payment_Type2</PaymentType>
          <PaymentTypeID>2</PaymentTypeID>
          <Setting>true</Setting>
        </Payment>
        .
        .
        .
        <Payment>
          <PaymentType>Payment_Type17</PaymentType>
          <PaymentTypeID>17</PaymentTypeID>
          <Setting>true</Setting>
        </Payment>
    </PaymentTypes>
</User>

I have created a class which holds all the xml elements as data members and array type for this calls我创建了一个 class ,其中包含所有 xml 元素作为此调用的数据成员和数组类型

public partial class PaymentTypes {
    
    private string paymentTypeField;
    
    private byte paymentTypeIDField;
    
    private bool settingField;
    
    /// <remarks/>
    public string PaymentType {
        get {
            return this.paymentTypeField;
        }
        set {
            this.paymentTypeField = value;
        }
    }
    
    /// <remarks/>
    public byte PaymentTypeID {
        get {
            return this.paymentTypeIDField;
        }
        set {
            this.paymentTypeIDField = value;
        }
    }
    
    /// <remarks/>
    public bool Setting {
        get {
            return this.settingField;
        }
        set {
            this.settingField = value;
        }
    }
}

private PaymentTypes[] paymentTypesField;
    
public PaymentTypes[] PaymentTypes {
    get {
        return this.paymentTypesField;
    }
    set {
        this.paymentTypesField = value;
    }
}

I am able to instantiate class and assign values..我能够实例化 class 并分配值..

PaymentTypes pt = new PaymentTypes();
pt.PaymentType = "Payment_Type1";
pt.PaymentTypeID = 1;
pt.Setting = true;

PaymentTypes pt1 = new PaymentTypes();
pt1.PaymentType = "Payment_Type2";
pt1.PaymentTypeID = 2;
pt1.Setting = true;

.
.
.

PaymentTypes pt16 = new PaymentTypes();
pt16.PaymentType = "Payment_Type17";
pt16.PaymentTypeID = 17;
pt16.Setting = true;

// Insert payment types into the array.
PaymentTypes[] paymentTypes = { pt, pt1,.....,pt16 };

//assign all 17 payment types to user object
user.PaymentTypes = paymentTypes;

by using xml serializer, I am able to generate the output xml as above.通过使用 xml 串行器,我能够生成 output xml 如上所述。


Drawback of the above code: it is not efficient.上述代码的缺点:效率不高。

  1. I have created an instance of PaymentTypes class 17 times, with different values我已经创建了一个PaymentTypes class 实例 17 次,具有不同的值
  2. 17 variables to hold different payment types 17 个变量来保存不同的支付类型
  3. more lines of code更多的代码行
  4. more time for execution更多的执行时间
  5. more memory consumption更多 memory 消费

Question : is there any better way to define all 17 payment types and assign to user object with minimal line of code or easy way or quick way?问题:有没有更好的方法来定义所有 17 种支付类型并分配给用户 object 以最少的代码行或简单的方法或快速的方法?

You can add a static Create() method that creates an instance for the class PaymentTypes and you can rewrite the properties in the contracted form like below.您可以添加一个 static Create() 方法,该方法为 class PaymentTypes 创建一个实例,您可以像下面这样以合同形式重写属性。

public partial class PaymentTypes {

    public static Create(byte paymentTypeID, string paymentType, bool setting)
    {
        return new PaymentTypes 
        {
            PaymentTypeID = paymentTypeID,
            PaymentType = paymentType,
            Setting = setting
        };
    }    

    /// <remarks/>
    public string PaymentType { get; set; }

    /// <remarks/>
    public byte PaymentTypeID { get; set; }

    /// <remarks/>
    public bool Setting { get; set; }
}

And then you can assign payment types to the user in this way:然后您可以通过这种方式将付款类型分配给用户:

//assign all 17 payment types to user object
user.PaymentTypes = new PaymentTypes[] 
{
    PaymentTypes.Create(1, "Payment_Type1", true),
    PaymentTypes.Create(2, "Payment_Type2", false),
    ....
    PaymentTypes.Create(17, "Payment_Type17", true)
};

Or without the Create() static method:或者没有 Create() static 方法:

//assign all 17 payment types to user object
user.PaymentTypes = new PaymentTypes[] 
{
    new PaymentTypes{ PaymentTypeID = 1, PaymentType = "PaymentType_1", Setting = true },
    new PaymentTypes{ PaymentTypeID = 2, PaymentType = "PaymentType_2", Setting = false },
    ....
    new PaymentTypes{ PaymentTypeID = 17, PaymentType = "PaymentType_17", Setting = true }
};

For the memory consumption I don't see any issue in your code because effectively you need to allocate 17 instances of the class PaymentType and since the classes are reference types they are not allocated in the stack memory.对于 memory 消耗,我在您的代码中看不到任何问题,因为实际上您需要分配 17 个 class PaymentType 实例,并且由于这些类是引用类型,因此它们未在堆栈 ZCD69B4957F06CD818D7BF3D6198 中分配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM