Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I import data on grid from xml file in vb.net at desktop application using visual studio 2005?
Posted
Updated 30-Nov-11 2:33am
v4
Comments
DaveAuld 29-Aug-11 7:14am    
Which languange? VB, C#, a.n.other? Which type of grid, Windows Forms, ASP, any other type?

______________________________________________________________________
VB
______________________________________________________________________
VB
Dim xDoc As System.Xml.Linq.XDocument = System.Xml.Linq.XDocument.Load("c:\test.xml")
Dim elements = From el In xDoc.Descendants("Master")
                Select Id = el.Element("Id").Value,
                       Name = el.Element("Name").Value
DataGridView1.DataSource = elements.ToList()
______________________________________________________________________
C#
______________________________________________________________________
C#
System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Load("c:\\test.xml");
var elements = from el in xDoc.Descendants("Master")
               select new
               {
                 Id = el.Element("Id").Value,
                 Name = el.Element("Name").Value
               };
datagridview.DataSource = elements.ToList();

______________________________________________________________________
test.xml
______________________________________________________________________
HTML
<test>
	<master>
		<id>1</id>
		<name>Name1</name>
	</master>
	<transaction>
		<id>1</id>
		<amount>100</amount>
	</transaction>
</test>
 
Share this answer
 
v3
Comments
RaisKazi 31-Aug-11 2:24am    
Perfect Answer. 5!
Hello Dear,

You can use the below code.

<pre lang="c#">
gridView.dataSource= Dataset.ReadXml(<XmlFilename>);
gridView.databind();

</pre>
 
Share this answer
 
You can use DataSet or DataTable's ReadXML method to load data from XML file to DataTable or DataSet and you can bind it to DataGrid or DataGridView.
 
Share this answer
 

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