简体   繁体   中英

Convert object property name to attribute and property value

I have this c# object with me

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

and would like to convert it in xml like this -

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

What about this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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