Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7069647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:16+00:00 2026-05-28T05:28:16+00:00

I have a heavily nested XML document that I need to load into my

  • 0

I have a heavily nested XML document that I need to load into my db for additional processing. For various reasons beyond the scope of this discussion I need to ‘flatten’ that structure down, then load it into a DataTables and then I can SQLBulkCopy it into the db where it will get processed. So assume my original XML looks something like this (mine is even more heavily nested, but this is the basic idea):

<data>
    <report id="1234" name="XYZ">
        <department id="234" name="Accounting">
            <item id="ABCD" name="some item">
                <detail id="detail1" value="1"/>                    
                <detail id="detail2" value="2"/>                    
                <detail id="detail3" value="3"/>                    
            </item>
        </department>      
    </report>
 </data>   

and I want to flatten that down into a single (albeit redundant) table structure where each attribute becomes a column (i.e. ReportId, ReportName, DepartmentId, DepartmentName, ItemId, ItemName, Detail1, Detail2, Detail3).

So my question is simply ‘is it possible to accomplish this with a simple Linq query’? In the past I would just write some XSLT and be done with it but I’m curious if the Linq library can accomplish the same thing?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:28:17+00:00Added an answer on May 28, 2026 at 5:28 am

    Is this what you’re looking for?

    var doc = XDocument.Load(fileName);
    var details =
        from report in doc.Root.Elements("report")
        from department in report.Elements("department")
        from item in department.Elements("item")
        from detail in item.Elements("detail")
        select new
        {
            ReportId = (int)report.Attribute("id"),
            ReportName = (string)report.Attribute("name"),
            DepartmentId = (int)department.Attribute("id"),
            DepartmentName = (string)department.Attribute("name"),
            ItemId = (string)item.Attribute("id"),
            ItemName = (string)item.Attribute("name"),
            DetailId = (string)detail.Attribute("id"),
            DetailValue = (int)detail.Attribute("value"),
        };
    

    If you want it as a DataTable, you can use the following extension method:

    public static DataTable ToDataTable<T>(this IEnumerable<T> source)
    {
        PropertyInfo[] properties = typeof(T).GetProperties()
                                             .Where(p => p.CanRead && !p.GetIndexParameters().Any())
                                             .ToArray();
    
        DataTable table = new DataTable();
        foreach (var p in properties)
        {
            Type type = p.PropertyType;
            bool allowNull = !type.IsValueType;
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                allowNull = true;
                type = Nullable.GetUnderlyingType(type);
            }
            DataColumn column = table.Columns.Add(p.Name, type);
            column.AllowDBNull = allowNull;
            column.ReadOnly = !p.CanWrite;
        }
    
        foreach (var item in source)
        {
            DataRow row = table.NewRow();
            foreach (var p in properties)
            {
                object value = p.GetValue(item, null) ?? DBNull.Value;
                row[p.Name] = value;
            }
            table.Rows.Add(row);
        }
    
        return table;
    }
    

    Use it like this:

    var table = details.CopyToDataTable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows service that is heavily multithreaded. Each thread calls various methods
I have code that relies heavily on yaml for cross-language serialization and while working
I have a rails app that uses heavily js (over 1MB total). I'd like
I have a web app that is heavily loaded in javascript and css. First
I have this c# web app that relies heavily on two dll files that
I have an iPhone app that heavily relies on the OpenCV library; as such,
We have an existing application that relies heavily on stored procedures and untyped DataSets.
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
I have the following (heavily simplified) model, that uses will_paginate class Search < ActiveRecord::Base
I have a webapplication that heavily uses JQuery. I would like to start using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.