[英]Need help on creating a generalized method for generating XML
我想寫一個通用的方法來使用C#創建xml文件。
假設我的XML Skeleton如下所示
<root>
<customer>
<mobile></mobile>
<email></email>
<external></external>
<firstname></firstname>
<lastname></lastname>
<gender></gender>
<registered></registered>
<custom>
<field>
<name></name>
<value></value>
</field>
<field>
<name></name>
<value></value>
</field>
</custom>
</customer>
</root>
如果我將傳遞包含所有必需值的對象,則我的方法應返回一個包含值的xml。
請注意:-我的xml結構將來可能會隨時更改。
目前,我已經創建了用於創建不同XML文件的單獨方法。因此,只要xml中發生任何更改,我都必須對方法進行更改並再次部署。
編輯:
我的客戶類別如下
class Customer
{
public string CustomerNumber { get; set; }
public string Title { get; set; }
public string county { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string companyname { get; set; }
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
public string AddressThree { get; set; }
public string city { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string email { get; set; }
public string RegisteredStore { get; set; }
public string mailable { get; set; }
public string rentable { get; set; }
public string emailable { get; set; }
public string JoinDate { get; set; }
public string CustomerExternalID { get; set; }
public string customergender { get; set; }
public string dateofbirth { get; set; }
public string homenumber { get; set; }
public string Mobile { get; set; }
//Custom Fields
public string CurrencyRef { get; set; }
public string InvitationStatus { get; set; }
public string LastMailDate { get; set; }
public string LastOrderDate { get; set; }
public string NearestShop { get; set; }
public string NearestShopDistance { get; set; }
public string Region { get; set; }
public string ShopperType { get; set; }
}
使用XElement我正在為客戶創建xml,但是在“自定義字段”中添加任何新字段時,我必須對代碼進行更改以獲取所需的xml輸出。
將對象轉換為XML的過程稱為“對象序列化”,即獲取內存中對象並將其轉換為可以寫入磁盤或通過網絡發送的XML的過程。 任何對象都可以序列化(轉換為XML或JSON)。
這是一個教程的鏈接,該教程顯示了如何在C#中執行此操作: http : //support.microsoft.com/kb/815813
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.