Skip to main content

Convert Nested XML to C# class object and C# Class object to Nested XML File

Convert Nested XML to C# class object and C# Class object to Nested XML File

  • Introduction
In this Article we learn how to deserialize XML to C# objects and reverse from C# objects to XML files. In this article we will do practical with example with demo class and xml files.

using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;

namespace TestApp
{
    public class Serializer
    {     
        public T Deserialize<T>(string input) where T : class
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (StringReader sr = new StringReader(input))
            {
                return (T)ser.Deserialize(sr);
            }
        }

        public string Serialize<T>(T ObjectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());

            using (StringWriter textWriter = new StringWriter())
            {
                xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                return textWriter.ToString();
            }
        }
    }
}

Comments

Popular posts from this blog

MVC Interview Questions

MVC Interview Questions What is MVC Application life cycle ? What is default route in MVC? What is Output Caching ? What are Route Constraints in MVC? What are the Benefits of Area in MVC? How do you implement Forms authentication in MVC? What are the Main Razor Syntax Rules? Define attribute based routing in MVC? What are HTML helpers in MVC? What does the MVC pattern define with 3 logical layers? What are the advantages of MVC? List the various return types of a controller action method. What are the types of filters? What are the Filters in MVC? What are the three segments for routing important? What is Route in MVC? what is the difference between ViewData and ViewBag? attribute based routing in MVC? What is GET and POST Actions Types? What is MVC (Model view controller)? What is Validation Summary in MVC? What’s new in the latest version of MVC? How to render html in Asp.net MVC view? Explain the concept of MVC Scaffolding? What is Bundling and Mini...

Angular Step By Step Installation Process

# Angular Step By Step  Hello Techies,        In this post i am going to guide you to install Angular and create and run your first app. Please follow bellow steps.. Step 1 (Node NPM Installation) Node Js ( NPM)  should be installed on system. So for installation of Node Js follow bellow steps. Open  https://nodejs.org/en  .  Download stable Version (LTS) of Node js. After complete the installation open CMD and check the version. Step : 2 (Install Angular by NPM) Type " npm install -g @angular/cli "  2.  Select folder where you are going to create Angular Project.  3. Type ng new 'your app-name' EX:  ng new my-dream-app And Relax it will take some couple of minute to create app , because its downloading all the package from server by npm in you local system. Type : " cd my-dream-app " to select project which is created.  Now Type " ng serve " to run the project...

Azure Service Bus Vs Azure Web Job

MENU Home SUBSCRIBE Today, we will be talking about Azure service bus and how to use them with Webjobs. Azure Service Bus  is a messaging infrastructure that sits between applications allowing them to exchange messages for improved scale and resiliency. It offers the following capabilities: Queues: offers simple first in, first out guaranteed message delivery. When to use Azure Service Bus? Service Bus queues are a general-purpose technology that can be used for a wide variety of scenarios: Communication between web and worker roles in a multi-tier Azure application Communication between on-premises apps and Azure hosted apps in a hybrid solution Communication between components of a distributed application running on-premises in different organizations or departments of an organization There are also Topics and Relay capabiliteis but we will talk about them in the future. A figure that illustrates how Azure Service Bus works. There is a sender who send a mes...