Deserialize XML with Array Node In C#

Ok lets begin. 

First of all, let's create an xml File: 

Open Notepad and paste the following code on it.

<?xml version="1.0" encoding="UTF-8"?>  
<note>  
<to>  
<email>[email protected]</email>  
<email>[email protected]</email>  
<email>[email protected]</email>  
</to>  
<from>[email protected]</from>  
<heading>this is the email heading</heading>  
</note>

Save the file as "xmlSample.xml" in any accesible directory.

Explanation of the xml file:

We have a root node named "note", inside of it we have 3 nodes (one of them is an array ("to" with many email elements inside), and the other two are single nodes("from, heading")).  

Now Open Visual Studio and create a new console application, give it any name you want.

Create a folder and give it the following name "XMLModels" and create a class named "Note" inside this folder.

 

The Note class will be our model to deserialize our xml file.

In order to make it work,m we should decorate it with some xml attributes as shown below: 

using System.Xml.Serialization;

namespace Deserialization.XMLModels
{    
    [XmlRoot(ElementName = "note")]  <- Root node name
    public class Note
    {         
        [XmlArray("to")]  <- Array Node element name
        [XmlArrayItem("email")]  <- Array item Element name
        public List<string> to { get; set; }  
        [XmlElement("from")]  <- Node Element
        public string from { get; set; }  
        [XmlElement(ElementName = "heading")]  <- Node element 
        public string heading { get; set; }  
    }   
}

NOTE: You can specify the node element name directly, or by the property [XmlElement("from")] or [XmlElement(ElementName = "from")] which is same.

As the node "to" is an array we should use XMLArray attribute, for nonArray elements we can use XMLElement.

Now paste the following code in  program class's Main method:

class Program  
{  
    static void Main(string[] args)  
    {  
        string file = @"C:\Users\Jose\Documents\csharpcorner\xmlSample.xml";  //<- your xml file full path  
          
        //Deserialization
        var xmlStr = File.ReadAllText(file);  // get text from xml file
        var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlStr));  // get bytes from that text
        XmlSerializer serializer = new XmlSerializer(typeof(Note));  // Initialize de serializer with the "Note" Type
        Note msn = ((Note)serializer.Deserialize(ms));  // call deserialization (Object result)  

        //code to loop and join the info in one string 
        string CompleteMsn = "";  
        CompleteMsn += "This email is comming from: " + msn.from +"."+ Environment.NewLine;  

        string toContacts = "";  
        foreach (var item in msn.to)toContacts += item + " | ";  

        CompleteMsn += "It will be sending to: " + toContacts +"."+ Environment.NewLine;  
        CompleteMsn += "The heading of the message is: " + msn.heading;

        //Print the string on the console  
        Console.WriteLine(CompleteMsn);  
        Console.ReadKey();  
    }  
}

And now you can run the app and the result is:

And joining all in a string variable the result is:

 

That's it. I hope you enjoyed this small tutorial about deserialization of xml. Thanks.