2 Replies - 13506 Views - Last Post: 20 March 2009 - 03:09 PM Rate Topic: -----

#1 Khaos2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-February 09

Display specific records in an XML file in a DataGridView

Post icon  Posted 20 March 2009 - 12:05 PM

I am trying to populate a DataGridView with all records in a remote XML file when a button is clicked, which I have accomplished thus far. Now I need to do the same thing but allow only records that match specific criteria to be displayed, such as displaying only records that have a date of say 03/20/2009. Here is my code for displaying all records:

		private void button1_Click(object sender, EventArgs e)
		{
			dataGridView1.Rows.Clear();

			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(@"\\server\incidents.xml");

			XmlNodeList xmlNumber = xmlDoc.GetElementsByTagName("Number");
			XmlNodeList xmlDescription = xmlDoc.GetElementsByTagName("Description");
			XmlNodeList xmlCustomer = xmlDoc.GetElementsByTagName("Customer");
			XmlNodeList xmlDate = xmlDoc.GetElementsByTagName("Date");
			XmlNodeList xmlStatus = xmlDoc.GetElementsByTagName("Status");

			int i = 0;

			while (i < xmlNumber.Count)
			{
				object[] data = { (object)xmlNumber.Item(i).InnerText,
								  (object)xmlDescription.Item(i).InnerText,
								  (object)xmlCustomer.Item(i).InnerText,
								  (object)xmlDate.Item(i).InnerText,
								  (object)xmlStatus.Item(i).InnerText };
				dataGridView1.Rows.Add(data);
				i++;



I've tried adding a "dataGridView1.Rows.Remove();" if a node doesn't match a specific value, but I just can't seem to get this filtering stuff to work. If someone could point me in the right direction it would be greatly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Display specific records in an XML file in a DataGridView

Posted 20 March 2009 - 01:41 PM

One option is to query the XML file, then populate a DataTable with the results. Here's an example of populating a DataTable with the search results from an XML file

public static DataTable GetRecordsByDate(string file, string date)
{
	//DataTable that will hold the found results
	DataTable results = new DataTable("SearchResults");
	//DataRow (used later)
	DataRow row = null;

	XmlDocument doc = new XmlDocument();
	doc.Load(file);

	//build the table
	results.Columns.Add("Number", typeof(int));
	results.Columns.Add("Description", typeof(string));
	results.Columns.Add("Customer", typeof(string));
	results.Columns.Add("Date", typeof(string));
	results.Columns.Add("Status", typeof(string));

	//** Change This **
	string query = "/Categories/Category";

	//now we loop through the list
	foreach(XmlNode node in doc.SelectNodes(query))
	{
		row = results.NewRow();

		//now we need to check for the proper date
		if(node.ChildNodes[3].InnerText.ToString() == date)
		{
			row[0] = Convert.ToInt32(node.ChildNodes[0].InnerText.ToString());
			row[1] = node.ChildNodes[1].InnerText.ToString();
			row[2] = node.ChildNodes[2].InnerText.ToString();
			row[3] = node.ChildNodes[3].InnerText.ToString();
			row[4] = node.ChildNodes[4].InnerText.ToString();

			//now add the row
			results.Rows.Add(row);
		}
	}

	//now return the DataTable
	return results;
}



Where it says //** Change This ** change it to your documents structure. This is the structure I'm working with for an example

<?xml version="1.0"?>
<Categories>
  <Category>
	<Language>C#</Language>
	<Tag>csharp</Tag>
  </Category>
  <Category>
	<Language>C++</Language>
	<Tag>cpp</Tag>
  </Category>
</Categories>



Which is why mine looks like /Categories/Category. You provide the value you're looking for and the file you're searching, and it returns a populated DataTable. Then you can reload your DataGridView like this

//First clear out the current rows
dataGridView1.dgv.Rows.Clear();

//create our DataTable
DataTable records = GetRecordsByDate("YourFile.xml", "03/20/2009");

//now re-populate
dataGridView1.DataSource = records.DefaultView;



Hope that helps :)
Was This Post Helpful? 1

#4 Khaos2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-February 09

Re: Display specific records in an XML file in a DataGridView

Posted 20 March 2009 - 03:09 PM

Thanks for the information. I got your example to work with my project, but its not exactly what I was looking for. I currently have static column names in my DataGridView and your example generates new ones in addition to those.

Is there a way to load my XML file with specific records right from the very beginning, similar to the method I am using to load all records?

Thanks again!


*** EDIT ***

I was able to figure out a different way achieve the end results I was looking for. Instead of filtering the XML contents when the XML file is loaded, I ended up doing this instead when my filter button is pressed:


XDocument xmlDoc = Xdocument.Load(@"\\server\incidents.xml");

var incidents = from incident in xmlDoc.Descendants("Incident")
	where incident.Element("Date").Value == cmbDate.SelectedItem.ToString()
	select new
				{
		Number = incident.Element("Number").Value,
		Description = incident.Element("Description").Value,
		Customer = incident.Element("Customer").Value,
		Date = incident.Element("Date").Value,
		Status = incident.Element("Status").Value
				};

dataGridView1.Rows.Clear();

foreach (var incident in incidents)
	{
						dataGridView1.Rows.Add(incident.Number, incident.Description, incident.Customer, incident.Date, incident.Status);
	}



This post has been edited by Khaos2009: 20 March 2009 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1