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

  • Home
  • SEARCH
  • 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 769413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:19:52+00:00 2026-05-14T18:19:52+00:00

Updated to be clear. Step One: I have a XML file that I want

  • 0

Updated to be clear.

Step One: I have a XML file that I want to load into a DatGridView. (Mostly working thanks to Max, but I still have a problem with the XML rollup)

Step Two: Run some code based on user input — (not part of this solution)

Step Three: Export the DataGridView into a CSV File. (Solved by Max! Thanks Man That was exactly what I was trying to do on that part.)

Using VS 2008 C#

(Created a new project)

Sample from the XML file cars.xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <car>
    <year>2010</year>
    <make>Chevy</make>
    <model>Surburban</model>
    <color-e>Black</color-e>
    <color-i>Black</color-i>
    <features>
      <Engine>8 cylinder</Engine>
      <gas>Petrol</gas>
      <doors>5</doors>
      <miles>12312</miles>
    </features>
    </car>
  <car>
    <year>2001</year>
    <make>Ford</make>
    <model>Excursion</model>
    <color-e>Black</color-e>
    <color-i>Black</color-i>
    <features>
      <Engine>10 cylinder</Engine>
      <gas>Petrol</gas>
      <doors>5</doors>
      <miles>90312</miles>
    </features>
  </car>
  <car>
    <year>1999</year>
    <make>Chevy</make>
    <model>corvette</model>
    <color-e>Silver</color-e>
    <color-i>Black</color-i>
    <features>
      <Engine>8 cylinder</Engine>
      <gas>Petrol</gas>
      <doors>3</doors>
      <miles>44222</miles>
    </features>
  </car>
</root>

This is a winform application.
It has two button, one textbox, and one datagridview.
button one should load the XML data into the datagrid.
Then button two should save the data in the datagridview to a CSV file.

Here is the code I have so far to open and load the xml into the datagridview.

namespace carsXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        var cars = XDocument.Load(@"C:\cars.xml");
        var query = from c in cars.Descendants("car")
                    select new
                    {
                        Year = (string)c.Element("year").Value,
                        Make = (string)c.Element("make").Value,
                        Model = (string)c.Element("model").Value,
                        // I needed to step directly into the sub element.
                        gas = (string)c.Element("features").Element("gas").Value,
                        doors = (string)c.Element("features").Element("doors").Value,
                        miles = (string)c.Element("features").Element("miles").Value


                    };

        dataGridView1.DataSource = query.ToList();

        }

        private void button2_Click(object sender, EventArgs e)
        {
          dataGridView1.ExportToCSV(@"C:\cars-griddump.csv"); 
         //Added Class Max showed me.  This works, I have only tested it on a small
         // XML file so far but it seems to work exactly as I wanted.
        }
    }
}

This gets the elements directly below the car element. When it gets to the feature element the application crashes. (null ref etc.)

So the last part of this before I am done with this little project is to figure out the XML rollup. When the code reads the car elements it gets all of the sub elements that are directly under car. It fails when it gets to the element that has additional sub elements and does not add them to the datagridview.

//Rollup problem fixed. It took me a while though!
// Thank you for all of the help!

  • 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-14T18:19:53+00:00Added an answer on May 14, 2026 at 6:19 pm

    Unless you want to use XSLT and XslCompiledTransform look at LINQ to XML. It is simple and strait forward. To load XML file you would use XDocument.Load(path) and to extract car element(s) you would use Decedents("car") method. And then simply loop over elements writing them to output.

    var cars = XDocument.Load(path).Decedents("car");
    

    LINQ to SQL

    XSLT and XslCompiledTransform: look for xslcompiledtransform on MSDN

    If you are using VS2008 you can extend DataGrid and create a helper class to handle all of your exporting needs.

    1. Add this [DataGridViewExtender] to your project as seen below

      
      namespace System.Windows.Forms
      {
      using System;
      using System.Collections.Generic;
      using System.Windows.Forms;
      using System.IO;

      static class DataGridViewExtender
      {
      public static void ExportToCSV(this DataGridView grid, string path)
      {
      ExportToFile(grid, path, ",");
      }

      public static void ExportToFile(this DataGridView grid, string path, string separator)
      {
      if (grid.Columns.Count <= 0)
      { return; }

      using (var writer = new StreamWriter(path))
      {
      var values = new List<string>(grid.Columns.Count);

      foreach (DataGridViewColumn column in grid.Columns)
      {
      values.Add(column.Name);
      }
      writer.WriteLine(string.Join(separator, values.ToArray()));
      foreach (DataGridViewRow row in grid.Rows)
      {
      values.Clear();
      foreach (DataGridViewCell cell in row.Cells)
      {
      values.Add(string.Format("{0}", cell.Value));
      }
      writer.WriteLine(string.Join(separator, values.ToArray()));
      }
      writer.Close();
      }
      }

      }
      }

    2. in button2_Click call this method


      private void button2_Click(object sender, EventArgs e)
      {
      dataGridView1.ExportToCSV(@"C:\griddump.csv");
      }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty sizeable object graph that I have serialized to a file
I have a site that I am currently working on in ASP.NET 2.0 using
Hoping to get some clear advice on this one. I want to push updates
UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom
UPDATED See post #3 below. There is a need to upload a file to
The question wasn't clear enough, I think; here's an updated straight to the point
I recently updated the configuration of one of my hudson builds. The build history
UPDATED I have made the changes to the C# code so it uses a
I have this new challenge to load ~100M rows from an Oracle database and
I have some data being loaded from a server, but there's no guarantee that

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.