---
title: "How do you deserialize an XML file into an object?"  
description: "How do you deserialize an XML file into an object?"  
author: "ICSM Computer"  
published: 2025-05-07  
updated: 2025-05-07  
canonical: https://www.mindstick.com/interview/34095/how-do-you-deserialize-an-xml-file-into-an-object  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 4 minutes  

---

# How do you deserialize an XML file into an object?

To **deserialize an XML file into an object** in C#, you typically use the `XmlSerializer` class from the `System.Xml.Serialization` namespace.

### Step-by-step: XML to Object

#### 1. Example XML (`person.xml`):

```xml
<Person>
  <Id>1</Id>
  <Name>John Doe</Name>
  <Email>john@example.com</Email>
</Person>
```

#### 2. Define the C# class:

```cs
using System;
using System.Xml.Serialization;

[XmlRoot("Person")]
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
```

#### 3. Deserialize the XML:

```cs
using System;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        string xmlPath = @"C:\person.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(Person));
        using (FileStream stream = new FileStream(xmlPath, FileMode.Open))
        {
            Person person = (Person)serializer.Deserialize(stream);
            Console.WriteLine($"{person.Id}: {person.Name} - {person.Email}");
        }
    }
}
```

#### Notes:

1. The XML element names must match the class property names (case-sensitive unless overridden with `[XmlElement("...")]`).
2. You can deserialize XML strings too using `StringReader` instead of `FileStream`.

### Deserialize a list of objects from XML into a `List<T>` in C# using `XmlSerializer`.

#### 1. Example XML with Multiple `<Person>` Elements

Save this as `people.xml`:

```xml
<People>
  <Person>
    <Id>1</Id>
    <Name>John Doe</Name>
    <Email>john@example.com</Email>
  </Person>
  <Person>
    <Id>2</Id>
    <Name>Jane Smith</Name>
    <Email>jane@example.com</Email>
  </Person>
</People>
```

#### 2. Define C# Classes to Match the XML

```cs
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

[XmlRoot("People")]
public class People
{
    [XmlElement("Person")]
    public List<Person> Persons { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
```

#### 3. Deserialize XML into a List of Person

```cs
using System;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        string xmlPath = @"C:\people.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(People));
        using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
        {
            People people = (People)serializer.Deserialize(fs);
            foreach (var person in people.Persons)
            {
                Console.WriteLine($"{person.Id}: {person.Name} - {person.Email}");
            }
        }
    }
}
```

#### Notes

1. The root element `<People>` maps to the `People` class.
2. Each `<Person>` becomes an item in the `List<Person>`.

## Answers

### Answer by ICSM Computer

To **deserialize an XML file into an object** in C#, you typically use the `XmlSerializer` class from the `System.Xml.Serialization` namespace.

### Step-by-step: XML to Object

#### 1. Example XML (`person.xml`):

```xml
<Person>
  <Id>1</Id>
  <Name>John Doe</Name>
  <Email>john@example.com</Email>
</Person>
```

#### 2. Define the C# class:

```cs
using System;
using System.Xml.Serialization;

[XmlRoot("Person")]
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
```

#### 3. Deserialize the XML:

```cs
using System;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        string xmlPath = @"C:\person.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(Person));
        using (FileStream stream = new FileStream(xmlPath, FileMode.Open))
        {
            Person person = (Person)serializer.Deserialize(stream);
            Console.WriteLine($"{person.Id}: {person.Name} - {person.Email}");
        }
    }
}
```

#### Notes:

1. The XML element names must match the class property names (case-sensitive unless overridden with `[XmlElement("...")]`).
2. You can deserialize XML strings too using `StringReader` instead of `FileStream`.

### Deserialize a list of objects from XML into a `List<T>` in C# using `XmlSerializer`.

#### 1. Example XML with Multiple `<Person>` Elements

Save this as `people.xml`:

```xml
<People>
  <Person>
    <Id>1</Id>
    <Name>John Doe</Name>
    <Email>john@example.com</Email>
  </Person>
  <Person>
    <Id>2</Id>
    <Name>Jane Smith</Name>
    <Email>jane@example.com</Email>
  </Person>
</People>
```

#### 2. Define C# Classes to Match the XML

```cs
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

[XmlRoot("People")]
public class People
{
    [XmlElement("Person")]
    public List<Person> Persons { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
```

#### 3. Deserialize XML into a List of Person

```cs
using System;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        string xmlPath = @"C:\people.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(People));
        using (FileStream fs = new FileStream(xmlPath, FileMode.Open))
        {
            People people = (People)serializer.Deserialize(fs);
            foreach (var person in people.Persons)
            {
                Console.WriteLine($"{person.Id}: {person.Name} - {person.Email}");
            }
        }
    }
}
```

#### Notes

1. The root element `<People>` maps to the `People` class.
2. Each `<Person>` becomes an item in the `List<Person>`.


---

Original Source: https://www.mindstick.com/interview/34095/how-do-you-deserialize-an-xml-file-into-an-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
