2

Default behaviour of JSON to XML serialization is arrays elements appear on the same level as non-array elements. Like on example from https://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm :

 {
  'Email': 'james@example.com',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
 }

is serialised into

<Root>
   <Email>james@example.com</Email>
   <Active>true</Active>
   <CreatedDate>2013-01-20T00:00:00Z</CreatedDate>
   <Roles>User</Roles>
   <Roles>Admin</Roles>
</Root>

But I need XML array in the form

<Roles>
  <Item>User</Item>
  <Item>Admin</Item>
</Roles>

How could I achieve this?

Thanks in advance.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17

1 Answers1

0

No problem!

var source = "(your JSON");
dynamic roles = JObject.Parse(source).Roles;   

Following this, you may re-serialize roles to XML, achieving the desired result.

Pancake
  • 739
  • 1
  • 5
  • 20