Creating and filling table from LINQ to XML in C#


Importing and parsing the attached XML file, I am trying to fill a newly created table from the data in the XML stream:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;
using System.Globalization;

namespace JobInvoicerCS
    public partial class Form1 : Form
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            // Create Dataset
            DataSet Members = new DataSet();

            // Fill tables and add them to dataset
            // Structure
            DataTable Beams = new DataTable();
            FillTableFromXML(Beams, "Beam".ToUpper());
            //Several tables not shown for clarity

        static public void FillTableFromXML(DataTable t, string Member)
            XDocument xdoc = XDocument.Load("Job.xml");
            t.Columns.Add("Piecemark", typeof(string));
            t.Columns.Add("TotalWeight", typeof(float));
            t.Columns.Add("Material", typeof(string));
            t.Columns.Add("Length", typeof(string));
            DataRow r = t.NewRow();

            var dummy = (from node in xdoc.Descendants("Detail")
                         let billLine = node.Element("BillLine")
                         let material = node.Element("BillLine").Element("Material")
                         where billLine.Attribute("Number").Value.Equals("1") && material.Value.Equals(Member)
                         float.Parse(billLine.Element("TotalWeight").Value, CultureInfo.InvariantCulture.NumberFormat),
                         node.Elements("BillLine").Where(b => b.Attribute("Number").Value.Equals("2")).SingleOrDefault().Element("Material").Value,
                         node.Elements("BillLine").Where(b => b.Attribute("Number").Value.Equals("2")).SingleOrDefault().Element("Length").Value

Open in new window

Using the above function, it should fill in the Piecemark from Line 1, and Material and Length from Line 2 of the XML if the Material value in Line 1 is "BEAM."

When I run it, nothing is added to the rows. What am I missing?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
This line of code,  Members.Tables.Add("Beams"); , should be this,  Members.Tables.Add(Beams);. Leave the quotes out..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
psdesignadminAuthor Commented:
Duh, should have seen that! Thanks a lot.
Fernando SotoRetiredCommented:
Not a problem psdesignadmin, that happens to all of us. Glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.