Deserialize XML with Array Node In C#

In this blog, I will explain how to deserialize an XML file with an array and single nodes.

Ok lets begin. 
 
First of all, let's create an xml File: 
 
Open Notepad and paste the following code on it.  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <note>  
  3. <to>  
  4. <email>juan@gmail.com</email>  
  5. <email>patrick@gmail.com</email>  
  6. <email>Rose@gmail.com</email>  
  7. </to>  
  8. <from>Jose@hotmail.com</from>  
  9. <heading>this is the email heading</heading>  
  10. </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: 
  1. using System.Xml.Serialization;
  2.  
  3. namespace Deserialization.XMLModels
  4. {    
  5.     [XmlRoot(ElementName = "note")]  <- Root node name
  6.     public class Note
  7.     {         
  8.         [XmlArray("to")]  <- Array Node element name
  9.         [XmlArrayItem("email")]  <- Array item Element name
  10.         public List<string> to { getset; }  
  11.         [XmlElement("from")]  <- Node Element
  12.         public string from { getset; }  
  13.         [XmlElement(ElementName = "heading")]  <- Node element 
  14.         public string heading { getset; }  
  15.     }   
  16. }
 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: 
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string file = @"C:\Users\Jose\Documents\csharpcorner\xmlSample.xml";  //<- your xml file full path  
  6.               
  7.             //Deserialization
  8.             var xmlStr = File.ReadAllText(file);  // get text from xml file
  9.             var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlStr));  // get bytes from that text
  10.             XmlSerializer serializer = new XmlSerializer(typeof(Note));  // Initialize de serializer with the "Note" Type
  11.             Note msn = ((Note)serializer.Deserialize(ms));  // call deserialization (Object result)  
  12.   
  13.             //code to loop and join the info in one string 
  14.             string CompleteMsn = "";  
  15.             CompleteMsn += "This email is comming from: " + msn.from +"."+ Environment.NewLine;  

  16.             string toContacts = "";  
  17.             foreach (var item in msn.to)toContacts += item + " | ";  
  18.   
  19.             CompleteMsn += "It will be sending to: " + toContacts +"."+ Environment.NewLine;  
  20.             CompleteMsn += "The heading of the message is: " + msn.heading;
  21.   
  22.             //Print the string on the console  
  23.             Console.WriteLine(CompleteMsn);  
  24.             Console.ReadKey();  
  25.         }  
  26.     }  
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.