2016-09-26 269 views
0

我有這個C#和我轉換對象的屬性名稱的屬性和屬性值

public class UserRoleAndGroupData 
{ 
    public Guid RoleId { get; set; } 
    public string RoleName { get; set; } 
    public int UserIntID { get; set; } 
} 

對象,並想將其轉換成XML這樣的 -

<add> 
    <doc> 
    <field name="RoleName">MyRole1</field> 
    <field name="RoleID">123</field> 
    <field name="UserIntId">567</field> 
    <doc> 
</add> 

回答

0

這個怎麼樣:

UserRoleAndGroupData u = new UserRoleAndGroupData(); 
... 
string template = [email protected]"<add> 
    <doc> 
    <field name="RoleName">{u.RoleName}</field> 
    <field name="RoleID">{u.RoleId}</field> 
    <field name="UserIntId">{u.UserIntID}</field> 
    <doc> 
</add>"; 
...