Convert XML File Data to a List Of Objects

Introduction

In this article, we will create an application that will get all data from XML files from a specific directory and convert it to a C# object. 
 
To get all XML files from a specific location we can use the Directory library provided by 'System.IO'. The other namespaces are 'System.Xml.Serialization' to serialize the XML. 
 
I have an XML file below:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <entry id="99" binderId="vg" definitionId="qwe" definitionName="asd" title="this is Title" exportVersion="3" docNumber="1" docLevel="1" href="google.com">  
  4.   <attribute name="_zone" type="text">abc</attribute>  
  5.   <signature>  
  6.     <creation date="2013-09-24T07:20:47">  
  7.       <principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>  
  8.     </creation>  
  9.     <modification date="2013-09-24T07:24:29">  
  10.       <principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>  
  11.     </modification>  
  12.   </signature>  
  13.   <attribute name="title" type="title">This is Title</attribute>  
  14.   <attribute name="description" type="description" format="1" zoneUUID="ff">This is desc.</attribute>  
  15.   <attribute name="attachFile" type="attachFiles">  
  16.     <file href="ar1.jpg" numVersions="1">ar1.jpg</file>  
  17.     <file href="ar2.jpg" numVersions="1">ar2.jpg</file>  
  18.     <file href="ar3.jpg" numVersions="1">ar3.jpg</file>  
  19.     <file href="ar4.jpg" numVersions="1">ar4.jpg</file>  
  20.     <file href="ar5.jpg" numVersions="1">ar5.jpg</file>  
  21.   </attribute>  
  22.   <workflows/>  
  23.   <settings>  
  24.     <accessControls/>  
  25.   </settings>  
  26. </entry>  
Now, we start to create a console application for transferring all data from XML files to C# objects.
 
Step 1 - Create a Project
 
After opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File - New - Project.
 
 
After choosing a project, a new dialog will pop up. In that, select Console Application and give your project the location and a name. Then, click the "Ok" button.
 
 
I create a class file like below to map all tags data to c# objects. 
  1. namespace Entry  
  2. {  
  3.     [XmlRoot(ElementName="attribute")]  
  4.     public class Attribute {  
  5.         [XmlAttribute(AttributeName="name")]  
  6.         public string Name { get; set; }  
  7.         [XmlAttribute(AttributeName="type")]  
  8.         public string Type { get; set; }  
  9.         [XmlText]  
  10.         public string Text { get; set; }  
  11.         [XmlAttribute(AttributeName="format")]  
  12.         public string Format { get; set; }  
  13.         [XmlAttribute(AttributeName="zoneUUID")]  
  14.         public string ZoneUUID { get; set; }  
  15.         [XmlElement(ElementName="file")]  
  16.         public List<File> File { get; set; }  
  17.     }  
  18.   
  19.     [XmlRoot(ElementName="principal")]  
  20.     public class Principal {  
  21.         [XmlAttribute(AttributeName="id")]  
  22.         public string Id { get; set; }  
  23.         [XmlAttribute(AttributeName="name")]  
  24.         public string Name { get; set; }  
  25.         [XmlAttribute(AttributeName="title")]  
  26.         public string Title { get; set; }  
  27.         [XmlAttribute(AttributeName="emailAddress")]  
  28.         public string EmailAddress { get; set; }  
  29.         [XmlText]  
  30.         public string Text { get; set; }  
  31.     }  
  32.   
  33.     [XmlRoot(ElementName="creation")]  
  34.     public class Creation {  
  35.         [XmlElement(ElementName="principal")]  
  36.         public Principal Principal { get; set; }  
  37.         [XmlAttribute(AttributeName="date")]  
  38.         public string Date { get; set; }  
  39.     }  
  40.   
  41.     [XmlRoot(ElementName="modification")]  
  42.     public class Modification {  
  43.         [XmlElement(ElementName="principal")]  
  44.         public Principal Principal { get; set; }  
  45.         [XmlAttribute(AttributeName="date")]  
  46.         public string Date { get; set; }  
  47.     }  
  48.   
  49.     [XmlRoot(ElementName="signature")]  
  50.     public class Signature {  
  51.         [XmlElement(ElementName="creation")]  
  52.         public Creation Creation { get; set; }  
  53.         [XmlElement(ElementName="modification")]  
  54.         public Modification Modification { get; set; }  
  55.     }  
  56.   
  57.     [XmlRoot(ElementName="file")]  
  58.     public class File {  
  59.         [XmlAttribute(AttributeName="href")]  
  60.         public string Href { get; set; }  
  61.         [XmlAttribute(AttributeName="numVersions")]  
  62.         public string NumVersions { get; set; }  
  63.         [XmlText]  
  64.         public string Text { get; set; }  
  65.     }  
  66.   
  67.     [XmlRoot(ElementName="settings")]  
  68.     public class Settings {  
  69.         [XmlElement(ElementName="accessControls")]  
  70.         public string AccessControls { get; set; }  
  71.     }  
  72.   
  73.     [XmlRoot(ElementName="entry")]  
  74.     public class Entry {  
  75.         [XmlElement(ElementName="attribute")]  
  76.         public List<Attribute> Attribute { get; set; }  
  77.         [XmlElement(ElementName="signature")]  
  78.         public Signature Signature { get; set; }  
  79.         [XmlElement(ElementName="workflows")]  
  80.         public string Workflows { get; set; }  
  81.         [XmlElement(ElementName="settings")]  
  82.         public Settings Settings { get; set; }  
  83.         [XmlAttribute(AttributeName="id")]  
  84.         public string Id { get; set; }  
  85.         [XmlAttribute(AttributeName="binderId")]  
  86.         public string BinderId { get; set; }  
  87.         [XmlAttribute(AttributeName="definitionId")]  
  88.         public string DefinitionId { get; set; }  
  89.         [XmlAttribute(AttributeName="definitionName")]  
  90.         public string DefinitionName { get; set; }  
  91.         [XmlAttribute(AttributeName="title")]  
  92.         public string Title { get; set; }  
  93.         [XmlAttribute(AttributeName="exportVersion")]  
  94.         public string ExportVersion { get; set; }  
  95.         [XmlAttribute(AttributeName="docNumber")]  
  96.         public string DocNumber { get; set; }  
  97.         [XmlAttribute(AttributeName="docLevel")]  
  98.         public string DocLevel { get; set; }  
  99.         [XmlAttribute(AttributeName="href")]  
  100.         public string Href { get; set; }  
  101.     }  
  102.   
  103. }  
If you don't want to create all properties manually then you can use the below link to generate C# class property from XML.

https://xmltocsharp.azurewebsites.net.
 
Step 3 - Create Method for Convert data from XML to C# object
 
Copy and paste the below code into your main method.
  1. string sourceFolderPath ="D:\Kaushik\AllFiles"  
  2.   
  3. if (Directory.Exists(sourceFolderPath))  
  4. {  
  5.     DirectoryInfo dirSource = new DirectoryInfo(sourceFolderPath);  
  6.     var allXMLFiles = dirSource.GetFiles("*.xml", SearchOption.AllDirectories).ToList();  
  7.   
  8.     List<Entry> listAllEntries = new List<Entry>();  
  9.   
  10.     foreach (var nextFile in allXMLFiles)  
  11.     {  
  12.         try  
  13.         {  
  14.             XmlSerializer serializer = new XmlSerializer(typeof(Entry));  
  15.             using (TextReader reader = new StringReader(System.IO.File.ReadAllText(nextFile.FullName)))  
  16.             {  
  17.                 Entry result = (Entry)serializer.Deserialize(reader);  
  18.                 listAllEntries.Add(result);  
  19.             }  
  20.         }  
  21.         catch (Exception ex)  
  22.         {  
  23.   
  24.         }  
  25.     }  
  26. }  
Please don't forget to change the path of your XML files' directory in variable "sourceFolderPath".
 
In the above code:
  1. var allXMLFiles = dirSource.GetFiles("*.xml", SearchOption.AllDirectories).ToList();  
Using this line we can get all XML files from a specific directory.
 
In this case, I get file content using "new StringReader(System.IO.File.ReadAllText(FileName))".
 
Using "(Entry)serializer.Deserialize(reader)" convert the specific file data to C# object according to created Entry class.
 
And after completing the foreach loop, you get the whole XML files data into listAllEntries object.


Recommended Free Ebook
Similar Articles