-1

I want to convert a complex object into XML data with one root node alone.

public class Customer
{
    public Address Address { get; set; }
    public User user { get; set; }
}

public class Address
{
    public string city { get; set; }
    public string State { get; set; }
}

public class User
{
    public string Name { get; set; }
    public string Id { get; set; }
    public Dictionary<String, String> response { get; set; }
}

I have customer class which contain Address and User property. I want to convert the Customer object into XML data with one root node.

I want the object into below XML format

<row>
    <cell cellType="city">Chennai</cell>
    <cell cellType="state">tamilnadu</cell>
    <cell cellType="name">test</cell>
    <cell cellType="id">001</cell>
    <cell cellType="response1">response1</cell>
    <cell cellType="response2">response2</cell>
</row>
Servy
  • 202,030
  • 26
  • 332
  • 449
Arun
  • 13
  • 5
  • Not related by why you using attributes for the name of value? Why not `Chennaitamilnadu...` and so on? – Fabio Jan 05 '17 at 17:26
  • 1
    Possible duplicate of [Serialize an object to XML](http://stackoverflow.com/questions/4123590/serialize-an-object-to-xml) – Daniel Corzo Jan 05 '17 at 20:31

2 Answers2

0

You can not do it with basic xml serialization in C#, but you can create dto class to achieve your goal. Use dto like this and map your entities to it:

[XmlRoot("row")]
public class RowDto
{
    [XmlElement("cell")]
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    [XmlAttribute("cellType")]
    public string Type { get; set; }

    [XmlText]
    public string Value { get; set; }
}
Yury Glushkov
  • 711
  • 6
  • 16
0

Try xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer()
            {
                Address = new Address()
                {
                    city = "Chennai",
                    State = "tamilnadu",
                },
                user = new User()
                {
                    Name = "test",
                    Id = "001",
                    response = new Dictionary<string, string>() {
                        { "response1", "response1"},
                        { "response2", "response2"}
                    }
                }
            };

            XElement row = new XElement("row", new object[] {
                new XElement("cell", new object[] {
                    new XAttribute("cellType", "city"),
                    customer.Address.city
                }),
                new XElement("cell", new object[] {
                        new XAttribute("cellType", "state"),
                        customer.Address.State
                    }),
                new XElement("cell", new object[] {
                        new XAttribute("cellType", "name"),
                        customer.user.Name
                    }),
                new XElement("cell", new object[] {
                        new XAttribute("cellType", "id"),
                        customer.user.Id
                    }),
                    customer.user.response.AsEnumerable().Select(x => new XElement("cell", new object[] {
                        new XAttribute("cellType",x.Key),
                        x.Value
                    }))
            });

         } 

        //<row>
        //    <cell cellType="city">Chennai</cell>
        //    <cell cellType="state">tamilnadu</cell>
        //    <cell cellType="name">test</cell>
        //    <cell cellType="id">001</cell>
        //    <cell cellType="response1">response1</cell>
        //    <cell cellType="response2">response2</cell>
        //</row>
    }
    public class Customer
    {
        public Address Address { get; set; }
        public User user { get; set; }
    }

    public class Address
    {
        public string city { get; set; }
        public string State { get; set; }
    }

    public class User
    {
        public string Name { get; set; }
        public string Id { get; set; }
        public Dictionary<String, String> response { get; set; }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20