Autogenerate DTO elements when loading an XML for Deserialization

1.2k views Asked by At

Suggest me a proper solution for autogenerating DTO elements when loading a proper XML for Deserialization.

This is my DTO

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

namespace GHelper.DTO
{
    public class ElementsDTO
    {
        [XmlRoot("GalLC")]
        public class FareBB
        {
            [xmlElement("Ip")]
            public string strIp { get; set; }
            [xmlElement("Port")]
            public int intPort { get; set; }
            [xmlElement("Type")]
            public int intPort{ get; set; }
            [xmlElement("Email")]
            public string strEmail{ get; set; }
        }
    }
}

Here is my XML

<GalLC>
    <Ip>192.168.2.100</Ip>
    <Port>5051</Port>
    <Type></Type>
    <Email></Email>
</GalLC>

The problem is: when i am getting a lengthy XML, I will consume lots of time to create a DTO for it. Please suggest me something about creating a DTO automatically.

1

There are 1 answers

2
Reddog On

Given that it sounds like you've worked out how to get an XML schema definition (XSD) you could use XSD.exe to generate the classes or there's even online tools for it... And if you want something really custom, you could look into T4 Text Templates.