Search code examples
xmltypesdatasetxsdcomplextype

C# Using datasets to read xml files with complex types into datagridview


I have a code as such that reads in an xml file into a dataset, then displays the data in a datagridview.
I have done some googling and I have found that my dataset reads in the xml, but only on the first "level" of elements.

It doesn't reach in far enough for the complex types. However it does save the complex types, or the "in-line" data, into the next table in the dataset. The xml file is all in one dataset, but the tables are split up depending on the complex types of the elements.

Is there any way to combine the tables into one big table so that I don't need to have a numeric up and down value to switch between table views?

For my program, I'm trying to make it easier for the user to be able to manipulate these xml files. Having one big table would be far better than a bunch of smaller ones.

Here is the code:

    string filename = Address.Text;
    dataGridView1.AutoGenerateColumns = true;
    DataSet dataSet1 = new DataSet("DEFAULT");
    dataSet1.ReadXml(filename, XmlReadMode.Auto);
    bindingSource1.DataSource = dataSet1.Tables[(int)tablenumber.value].DefaultView;
    dataGridView1.DataSource = bindingSource1;
    richTextBox1.Text = filename + " loaded.";
    label2.Text = "File Open: " + filename;

and so on, but the tablenumber would be 0 to start off with. This is just the "first" table that the dataset has when it reads in the xml file. Also, I do have a schema that goes with the xml files. Would I need to read the schema as well just so it knows that there's a complex type and will read according to the schema?

(.NET 3.5 SP1, Visual Studio 2008 C#)


Solution

  • This question was solved by using a for loop that iterated through each table in the dataset and merging it. This produced a larger datatable that suits the datagridview regardless of the schema. There were void cells, which is expected, but that was exactly what I wanted: a huge datatable.

        DataTable dt = new DataTable();
        dt = dataSet1.Tables[0];
        int i;
        for(i = 1; i < dataSet1.Tables.Count; i++)
        {
            dt.Merge(dataSet.Tables[i]);
        }
    

    Was the code I used. =)

    Edit: More information (restrictions, example) Table Merge