Suppose I am having one small XML Document,
and I wanted to show contents of XML Document in Datagridview,
So How can I............?
for example the XML document is :-
and these EmpId and EmpName is wanted to shown in shown in dataGrid.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 2:49 AM EST
Finally I understood your requirement.
Check this code.
Please check 'DO YOU LIKE THIS ANSWER' checkbox if this helps you.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 17, 2009, 9:39 PM EST
I wrote a small application to read your xml file and display it in a datagridview.
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.Xml; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); dataGridView1.Columns.Add("col1", "ID"); dataGridView1.Columns.Add("col2", "Name"); } private void button1_Click(object sender, EventArgs e) { XmlTextReader textReader = new XmlTextReader("C:\\abc.xml");//Path of the xml file XmlDocument doc = new XmlDocument(); textReader.Read(); doc.Load(textReader); dataGridView1.Rows.Clear(); XmlNodeList employeeDetailNodes = doc.ChildNodes; foreach (XmlNode node in employeeDetailNodes) { XmlNodeList employeeIDs = node.ChildNodes; foreach (XmlNode idset in employeeIDs) { XmlNodeList ids = idset.ChildNodes; int j = 0; foreach (XmlNode id in ids) { int index; if (idset.Name.Equals("EmployeeID")) { index = dataGridView1.NewRowIndex; dataGridView1.Rows.Add(); if (dataGridView1.Rows[index] != null) { dataGridView1.Rows[index].Cells[0].Value = id.InnerText; } } else { index = j; if (dataGridView1.Rows[index] != null) { dataGridView1.Rows[index].Cells[1].Value = id.InnerText; } j++; } } } } } } }Run this and check.
Please do not forget to check 'Do you like this answer' check box if this helps you.
Ankit Rathore64 reputationPosted Dec 18, 2009, 2:11 AM EST
please provide me another solutions.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 18, 2009, 2:27 AM EST
Sorry. My previous answer is not in good foramt. So I uploaded the code.
Check it. (Change the file path to your xml file path.)
Please do not forget to check 'Do you like this answer' checkbox if it helps you.
Ankit Rathore64 reputationPosted Dec 18, 2009, 7:12 PM EST
Thanks for the reply but I wanted to do in simple manner,
by using like DATASET then DATABIND then show it in DataGridView
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 19, 2009, 2:44 AM EST
I can do what you want but you need to correctly as your XML should like below to retrieve the information efficiently
your xml should be like in below format ..
if you want code for below format then let me know i will help you :)
will provide you very simpler code that you can understand :)
Ankit Rathore64 reputationPosted Dec 19, 2009, 2:51 AM EST
I cant change the format
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 19, 2009, 3:32 AM EST
Ankit Rathore64 reputationPosted Dec 21, 2009, 12:44 AM EST
The more EMPLOYEE ID also may happen .............
Ankit Rathore64 reputationPosted Dec 21, 2009, 12:59 AM EST
now I am facing a problem in which I have more then two child node then the output is not coming,
Here the code is meant for two child node only ..........
so please help me to give this below XML document ..............
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 1:18 AM EST
Please do not forget to tick 'Do you like this answer' check box, if this helps you.
I have the solution for you. Check my code.
Ankit Rathore64 reputationPosted Dec 21, 2009, 6:54 PM EST
Suppose I am having very Big xml document in which lots of child nodes available in between I have to choose only 3 or 4 so is it possible .........to do that ..........?
I have to choose only 3 or 4 child node, So how can I ........?
Please Help me out ..........
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 21, 2009, 7:06 PM EST
As this is a separate question, would you mind post this in a separate thread? So it is easier to both of us. And also, this requirement is not clear for me.
You always have to read column 3 and 4 or it varies?
With the answers for these questions, can you post this in a separate thread?