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 C# object.
Step 1
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 a XML file like the below sample.xml.
<?xml version="1.0" encoding="UTF-8"?>
<entry id="99" binderId="vg" definitionId="qwe" definitionName="asd" title="this is Title" exportVersion="3" docNumber="1" docLevel="1" href="google.com">
<attribute name="_zone" type="text">abc</attribute>
<signature>
<creation date="2013-09-24T07:20:47">
<principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>
</creation>
<modification date="2013-09-24T07:24:29">
<principal id="1" name="admin" title="admin" emailAddress="[email protected]">admin</principal>
</modification>
</signature>
<attribute name="title" type="title">This is Title</attribute>
<attribute name="description" type="description" format="1" zoneUUID="ff">This is desc.</attribute>
<attribute name="attachFile" type="attachFiles">
<file href="ar1.jpg" numVersions="1">ar1.jpg</file>
<file href="ar2.jpg" numVersions="1">ar2.jpg</file>
<file href="ar3.jpg" numVersions="1">ar3.jpg</file>
<file href="ar4.jpg" numVersions="1">ar4.jpg</file>
<file href="ar5.jpg" numVersions="1">ar5.jpg</file>
</attribute>
<workflows/>
<settings>
<accessControls/>
</settings>
</entry>
Step: 2 Create a Project
Now, we start to create a console application to get all data from XML to c# objects.
After opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File - New - Project.
Create a class file to map above all tags you can also use the sites which provide the XML to c# property class. In this case, I use https://xmltocsharp.azurewebsites.net.
It will show like below,
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="attribute")]
public class Attribute {
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
[XmlAttribute(AttributeName="format")]
public string Format { get; set; }
[XmlAttribute(AttributeName="zoneUUID")]
public string ZoneUUID { get; set; }
[XmlElement(ElementName="file")]
public List<File> File { get; set; }
}
[XmlRoot(ElementName="principal")]
public class Principal {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="title")]
public string Title { get; set; }
[XmlAttribute(AttributeName="emailAddress")]
public string EmailAddress { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="creation")]
public class Creation {
[XmlElement(ElementName="principal")]
public Principal Principal { get; set; }
[XmlAttribute(AttributeName="date")]
public string Date { get; set; }
}
[XmlRoot(ElementName="modification")]
public class Modification {
[XmlElement(ElementName="principal")]
public Principal Principal { get; set; }
[XmlAttribute(AttributeName="date")]
public string Date { get; set; }
}
[XmlRoot(ElementName="signature")]
public class Signature {
[XmlElement(ElementName="creation")]
public Creation Creation { get; set; }
[XmlElement(ElementName="modification")]
public Modification Modification { get; set; }
}
[XmlRoot(ElementName="file")]
public class File {
[XmlAttribute(AttributeName="href")]
public string Href { get; set; }
[XmlAttribute(AttributeName="numVersions")]
public string NumVersions { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="settings")]
public class Settings {
[XmlElement(ElementName="accessControls")]
public string AccessControls { get; set; }
}
[XmlRoot(ElementName="entry")]
public class Entry {
[XmlElement(ElementName="attribute")]
public List<Attribute> Attribute { get; set; }
[XmlElement(ElementName="signature")]
public Signature Signature { get; set; }
[XmlElement(ElementName="workflows")]
public string Workflows { get; set; }
[XmlElement(ElementName="settings")]
public Settings Settings { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="binderId")]
public string BinderId { get; set; }
[XmlAttribute(AttributeName="definitionId")]
public string DefinitionId { get; set; }
[XmlAttribute(AttributeName="definitionName")]
public string DefinitionName { get; set; }
[XmlAttribute(AttributeName="title")]
public string Title { get; set; }
[XmlAttribute(AttributeName="exportVersion")]
public string ExportVersion { get; set; }
[XmlAttribute(AttributeName="docNumber")]
public string DocNumber { get; set; }
[XmlAttribute(AttributeName="docLevel")]
public string DocLevel { get; set; }
[XmlAttribute(AttributeName="href")]
public string Href { get; set; }
}
}