Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# Convert xml to datatable

/// <summary>
        /// Convert XML to DataTable
        /// </summary>
        /// <param name="filePath">XML FIle Path</param>
        /// <returns>DataTable</returns>
        public DataTable Read(string filePath)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(filePath);
                XmlNode parent = document.DocumentElement.ChildNodes.Cast<XmlNode>().ToList()[0];
                //Column 
                foreach (XmlNode column in parent)
                {
                    dt.Columns.Add(column.Name, typeof(string));
                }

                XmlElement root = document.DocumentElement;
                XmlNodeList childNodes = root?.ChildNodes;

                for (int i =0; i <= childNodes.Count - 1; i++)
                {
                    XmlNode child = document.DocumentElement.Cast<XmlNode>().ToList()[i];
                    List<string> rContent = new List<string>();
                    //rows
                    foreach (XmlNode item in child)
                    {
                        rContent.Add(item.InnerText);
                    }

                    dt.Rows.Add(rContent.ToArray());
                    rContent.Clear();
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
Comment

PREVIOUS NEXT
Code Example
Csharp :: maximum sum subarray c# 
Csharp :: datetimeoffset to datetime 
Csharp :: c# console.writeline 
Csharp :: c# HttpResponseMessage postResponse = client.PostAsync 
Csharp :: how to make a cast in c# 
Csharp :: dbset syntax 
Csharp :: get xml from url 
Csharp :: c# distinct comparer multiple properties 
Csharp :: c# nunit test case 
Csharp :: triangle 
Csharp :: link list in c# 
Csharp :: what are delegates and how to use them c# 
Csharp :: dateTime first/last 
Csharp :: non null array length 
Csharp :: void on TriggerCollisionEnter2D 
Csharp :: mongodb custom IIdGenerator 
Csharp :: asp.net store list in web.config 
Csharp :: command parameter wpf 
Csharp :: erewt 
Csharp :: deleting an item from a vector c# 
Csharp :: camera is rendering black screen unity 
Csharp :: c# XmlElement from string 
Csharp :: cannot convert from group method to threadstart C# 
Csharp :: array of objects c# 
Csharp :: add dynamic value in startup file in .net core api 
Csharp :: csgo crashes at retrieving game data 
Csharp :: f# print array strings 
Csharp :: imageLocation in C# 
Csharp :: == vs equals c# 
Csharp :: visibility bound to radio button wpf 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =