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. <entry id="99" binderId="vg" definitionId="qwe" definitionName="asd" title="this is Title" exportVersion="3" docNumber="1" docLevel="1" href="google.com">
  3. <attribute name="_zone" type="text">abc</attribute>
  4. <signature>
  5. <creation date="2013-09-24T07:20:47">
  6. <principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>
  7. </creation>
  8. <modification date="2013-09-24T07:24:29">
  9. <principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>
  10. </modification>
  11. </signature>
  12. <attribute name="title" type="title">This is Title</attribute>
  13. <attribute name="description" type="description" format="1" zoneUUID="ff">This is desc.</attribute>
  14. <attribute name="attachFile" type="attachFiles">
  15. <file href="ar1.jpg" numVersions="1">ar1.jpg</file>
  16. <file href="ar2.jpg" numVersions="1">ar2.jpg</file>
  17. <file href="ar3.jpg" numVersions="1">ar3.jpg</file>
  18. <file href="ar4.jpg" numVersions="1">ar4.jpg</file>
  19. <file href="ar5.jpg" numVersions="1">ar5.jpg</file>
  20. </attribute>
  21. <workflows/>
  22. <settings>
  23. <accessControls/>
  24. </settings>
  25. </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. [XmlRoot(ElementName="principal")]
  19. public class Principal {
  20. [XmlAttribute(AttributeName="id")]
  21. public string Id { get; set; }
  22. [XmlAttribute(AttributeName="name")]
  23. public string Name { get; set; }
  24. [XmlAttribute(AttributeName="title")]
  25. public string Title { get; set; }
  26. [XmlAttribute(AttributeName="emailAddress")]
  27. public string EmailAddress { get; set; }
  28. [XmlText]
  29. public string Text { get; set; }
  30. }
  31. [XmlRoot(ElementName="creation")]
  32. public class Creation {
  33. [XmlElement(ElementName="principal")]
  34. public Principal Principal { get; set; }
  35. [XmlAttribute(AttributeName="date")]
  36. public string Date { get; set; }
  37. }
  38. [XmlRoot(ElementName="modification")]
  39. public class Modification {
  40. [XmlElement(ElementName="principal")]
  41. public Principal Principal { get; set; }
  42. [XmlAttribute(AttributeName="date")]
  43. public string Date { get; set; }
  44. }
  45. [XmlRoot(ElementName="signature")]
  46. public class Signature {
  47. [XmlElement(ElementName="creation")]
  48. public Creation Creation { get; set; }
  49. [XmlElement(ElementName="modification")]
  50. public Modification Modification { get; set; }
  51. }
  52. [XmlRoot(ElementName="file")]
  53. public class File {
  54. [XmlAttribute(AttributeName="href")]
  55. public string Href { get; set; }
  56. [XmlAttribute(AttributeName="numVersions")]
  57. public string NumVersions { get; set; }
  58. [XmlText]
  59. public string Text { get; set; }
  60. }
  61. [XmlRoot(ElementName="settings")]
  62. public class Settings {
  63. [XmlElement(ElementName="accessControls")]
  64. public string AccessControls { get; set; }
  65. }
  66. [XmlRoot(ElementName="entry")]
  67. public class Entry {
  68. [XmlElement(ElementName="attribute")]
  69. public List<Attribute> Attribute { get; set; }
  70. [XmlElement(ElementName="signature")]
  71. public Signature Signature { get; set; }
  72. [XmlElement(ElementName="workflows")]
  73. public string Workflows { get; set; }
  74. [XmlElement(ElementName="settings")]
  75. public Settings Settings { get; set; }
  76. [XmlAttribute(AttributeName="id")]
  77. public string Id { get; set; }
  78. [XmlAttribute(AttributeName="binderId")]
  79. public string BinderId { get; set; }
  80. [XmlAttribute(AttributeName="definitionId")]
  81. public string DefinitionId { get; set; }
  82. [XmlAttribute(AttributeName="definitionName")]
  83. public string DefinitionName { get; set; }
  84. [XmlAttribute(AttributeName="title")]
  85. public string Title { get; set; }
  86. [XmlAttribute(AttributeName="exportVersion")]
  87. public string ExportVersion { get; set; }
  88. [XmlAttribute(AttributeName="docNumber")]
  89. public string DocNumber { get; set; }
  90. [XmlAttribute(AttributeName="docLevel")]
  91. public string DocLevel { get; set; }
  92. [XmlAttribute(AttributeName="href")]
  93. public string Href { get; set; }
  94. }
  95. }
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. if (Directory.Exists(sourceFolderPath))
  3. {
  4. DirectoryInfo dirSource = new DirectoryInfo(sourceFolderPath);
  5. var allXMLFiles = dirSource.GetFiles("*.xml", SearchOption.AllDirectories).ToList();
  6. List<Entry> listAllEntries = new List<Entry>();
  7. foreach (var nextFile in allXMLFiles)
  8. {
  9. try
  10. {
  11. XmlSerializer serializer = new XmlSerializer(typeof(Entry));
  12. using (TextReader reader = new StringReader(System.IO.File.ReadAllText(nextFile.FullName)))
  13. {
  14. Entry result = (Entry)serializer.Deserialize(reader);
  15. listAllEntries.Add(result);
  16. }
  17. }
  18. catch (Exception ex)
  19. {
  20. }
  21. }
  22. }
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.