How to convert an XML file to a Dynamic object using C# in .NET?

5
(7)

How to convert an XML file to a Dynamic object using C# in .NET?

This article is all about How to convert an XML file to a Dynamic object using C# in .NET? You may have a related question “How to get the data from an XML file using DynamicObject Class?”

Please note, this application is created using .NET 6.

Prerequisites

What is a Dynamic object?

As per the Microsoft doc – Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.

Create a Sample XML file

The first step is to create an XML file that contains some data. In our case, I have one XML file that contains employee data. here is the sample XML file. you can also copy this data and create a sample XML file for the practice.

<?xml version="1.0" standalone="yes"?>
<Jignesh>
	<Row>
		<EmployeeId>001</EmployeeId>
		<EmployeeName>Jignesh</EmployeeName>
		<EmployeeRole>Software Developer</EmployeeRole>
		<EmployeeEmail>[email protected]</EmployeeEmail>
		<EmployeeLocation>Pune</EmployeeLocation>
	</Row>
	<Row>
		<EmployeeId>002</EmployeeId>
		<EmployeeName>Tony</EmployeeName>
		<EmployeeRole>Software Developer</EmployeeRole>
		<EmployeeEmail>[email protected]</EmployeeEmail>
		<EmployeeLocation>Pune</EmployeeLocation>
	</Row>
	<Row>
		<EmployeeId>003</EmployeeId>
		<EmployeeName>John</EmployeeName>
		<EmployeeRole>Software Developer</EmployeeRole>
		<EmployeeEmail>[email protected]</EmployeeEmail>
		<EmployeeLocation>Pune</EmployeeLocation>
	</Row>
	<Row>
		<EmployeeId>004</EmployeeId>
		<EmployeeName>Tom</EmployeeName>
		<EmployeeRole>Software Developer</EmployeeRole>
		<EmployeeEmail>[email protected]</EmployeeEmail>
		<EmployeeLocation>Pune</EmployeeLocation>
	</Row>
</Jignesh>

The complete code

Here we browse the XML file created as above and then convert the file into a dynamic object. You are required to add namespace and Newtonsoft.Json package to run the code.

using Newtonsoft.Json;
using System.Dynamic;
using System.Xml.Linq;

namespace Zenkins.DynamicFormDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var filePath = openFileDialog1.FileName;
                XDocument doc = XDocument.Load(filePath);
                string jsonText = JsonConvert.SerializeXNode(doc);
                dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
                foreach (dynamic obj in dyn.Jignesh)
                {
                    var row = obj.Value as dynamic;
                    foreach (var rw in row)
                    {
                        try
                        {
                            //Implement code to perform operation with each row object
                        }
                        catch { }

                    }
                }
            }
        }
    }
}

In the above code, we browse the file and convert it to jsonText using JsonConvert.SerializeXNode. Once we get the jsonText we use JsonConvert.DeserializeObject and convert our jsonText to System.Dynamic.ExpandoObject type.

Here dyn.Jignesh is the key value for the root node in the XML file.

var row = obj.Value as dynamic; This line gives a dynamic object for each row

Output

In the above screenshot, you can check, that there are a total of 4 (0 to 3) System.Dynamic.ExpandoObject object for each row from the XML file.

In the above screenshot, you can check, that we get a dynamic object as “rw” which contains properties like “EmployeeId”, “EmployeeName”, “EmployeeRole”, “EmployeeEmail”, and “EmployeeLocation”. You can implement your custom login within foreach loop for each dynamic object.

I hope, this article gives your a brief idea about converting an XML file to a Dynamic object using C# in .NET or getting the data from an XML file using DynamicObject Class.

Happy Coding!

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?