Click here to Skip to main content
15,915,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am importing xml file data in dataset and displays in datagridview.

C#
{
openFileDialog1.Multiselect = true;
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                txtXML.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                DataSet ds = new DataSet();
                ds.ReadXml(openFileDialog1.FileName.ToString());
                dataGridView1.DataSource = ds.Tables[0];  -----------ERRRor
}


when i open one xml file named as AQList.xml
it shows only first row when i write as dataGridView1.DataSource = ds.Tables[0];----Only display first row

and when i write as dataGridView1.DataSource = ds.Tables[2];-------Display all data.


My xml file is

XML
<?xml version="1.0" encoding="UTF-8"?>
-<CONSOLIDATED_LIST dateGenerated="2015-01-23T00:00:00"> -<INDIVIDUALS> -<INDIVIDUAL> <DataId>1</DataId> <Name>Hitesh</Name> <Address>Mumbai</Address> </INDIVIDUAL> -<INDIVIDUAL> <DataId>2</DataId> <Name>Jitesh</Name> <Address>Mumbai</Address> </INDIVIDUAL> -<INDIVIDUAL> <DataId>3</DataId> <Name>Ritesh</Name> <Address>Mumbai</Address> </INDIVIDUAL> -<INDIVIDUAL> <DataId>4</DataId> <Name>Mitesh</Name> <Address>Mumbai</Address> </INDIVIDUAL> -<INDIVIDUAL> <DataId>5</DataId> <Name>Nitesh</Name> <Address>Mumbai</Address> </INDIVIDUAL> </INDIVIDUALS> -<ENTITY> -<ENTITIES> <DataId>11</DataId> <Name>Hitesh</Name> <Address>Mumbai</Address> </ENTITIES> -<ENTITIES> <DataId>12</DataId> <Name>Hitesh</Name> <Address>Mumbai</Address> </ENTITIES> -<ENTITIES> <DataId>13</DataId> <Name>Hitesh</Name> <Address>Mumbai</Address> </ENTITIES> </ENTITY> </CONSOLIDATED_LIST>
Posted
Updated 19-Feb-15 0:55am
v3
Comments
Sergey Alexandrovich Kryukov 19-Feb-15 2:23am    
What is that question title supposed to mean? "Sandwich does not eat cheese", honestly.
And what "FileName.ToString()" is supposed to mean? "Convert" a string into identical string...
I mean, oh... do you really think you are ready to deal with databases? I would hold on on that, to get confidences with the basics first...

As to your question: who knows what's there, in this XML file..?

—SA
Member 10488391 19-Feb-15 2:41am    
Have tried by passing the name of .xml file manually ?
Is it Working as expected ?
What data your .xml file contain ?
John C Rayan 19-Feb-15 5:23am    
Need to know structure of the XML you are reading. From what you say I can only guess that your XML more data than you expect.
Jayesh27j 19-Feb-15 6:55am    
-<consolidated_list dategenerated="2015-01-23T00:00:00"> -<individuals> -<individual> <dataid>1 <name>Hitesh
Mumbai
-<individual> <dataid>2 <name>Jitesh
Mumbai
-<individual> <dataid>3 <name>Ritesh
Mumbai
-<individual> <dataid>4 <name>Mitesh
Mumbai
-<individual> <dataid>5 <name>Nitesh
Mumbai
-<entity> -<entities> <dataid>11 <name>Hitesh
Mumbai
-<entities> <dataid>12 <name>Hitesh
Mumbai
-<entities> <dataid>13 <name>Hitesh
Mumbai
John C Rayan 19-Feb-15 7:56am    
Hi, I could see ds creates 5 tables for each high level nodes and you have to use table 3 (.Tables[2]) to point to Individual nodes. You are using correctly. What are you trying to do? Are you trying to use Tables[0] and why?

1 solution

I've written an example to show you what is going on.

Using this xml snippet
XML
<?xml version="1.0" encoding="UTF-8"?>
<level1 dateGenerated="2015-01-23T00:00:00">
    <level2>
        <level3>some data</level3>
        <level3>some more data</level3>
    </level2>
</level1>

This code
C#
DataSet ds = new DataSet();
ds.ReadXml(@"c:\temp\test2.xml");
for (var i = 0; i < ds.Tables.Count; i++)
{
    var dt = ds.Tables[i];
    Console.WriteLine("Table {0} - {1} : {2}", i, dt.TableName, dt.Rows.Count);
}
produces this output
Table 0 - level1 : 1
Table 1 - level2 : 1
Table 2 - level3 : 2
As John C Rayan has pointed out - you get a table per high-level node.
But notice what happens if you remove the dateGenerated attribute...
XML
<?xml version="1.0" encoding="UTF-8"?>
<level1>
    <level2>
        <level3>some data</level3>
        <level3>some more data</level3>
    </level2>
</level1>
Now when you run the above code you get these results
Table 0 - level2 : 1
Table 1 - level3 : 2
The highest level node "level1" no longer contains anything of interest and only the subsequent nodes are read into tables.

If I now add some code to dig into those tables
C#
for (var j = 0; j < dt.Rows.Count; j++)
    Console.WriteLine("    Data: {0}", dt.Rows[j].ItemArray[0]);

I get the following output
Table 0 - level2 : 1
    Data: 0
Table 1 - level3 : 2
    Data: some data
    Data: some more data

If I push your original xml through my little program I get this
Table 0 - CONSOLIDATED_LIST : 1
    Data: 0 2015-01-23T00:00:00
Table 1 - INDIVIDUALS : 1
    Data: 0 0
Table 2 - INDIVIDUAL : 5
    Data: 1 Hitesh
    Data: 2 Jitesh
    Data: 3 Ritesh
    Data: 4 Mitesh
    Data: 5 Nitesh
Table 3 - ENTITY : 1
    Data: 0 0
Table 4 - ENTITIES : 3
    Data: 11 Hitesh
    Data: 12 Hitesh
    Data: 13 Hitesh

Essentially ReadXml is attempting to mimic the "structure" of your xml file in the absence of a schema. More details here[^]
 
Share this answer
 
Comments
Jayesh27j 20-Feb-15 1:26am    
i want to display this xml data in datagridview.I have tried your code,but it couldn't help me.
CHill60 20-Feb-15 4:27am    
You've already stated that when you use ds.Tables[2] then data is displayed in the datagridview. I was trying to demonstrate why the data is in Tables[2] and not in Tables[1]. If you also want to display the ENTITIES entries then you will need to merge Tables[2] and Tables[4] - but be aware this would be displaying different things together in the DGV.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900