Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
4.00/5 (5 votes)
See more:
I have been required for the first time to work directly with .xml files in C# I have tried multiple solutions but all of them have entirely botched my code except for my most recent attempt which while it doesn't stop the program from running it fails to do what I wanted.
I need to load the xml file who's path is stored in strPathtoRegion and get the various data to be stored in a table. I have a form with a DataGridView object named RegionDataSet which I want the information to be displayed in. For some reason however, the code executes and the window remains an unaffected grey.

Why is this happening? Is there a better a way to do this? A large quantity of searches have yielded different pieces of code but none of which have really worked.

C#
public void XMLManager(string strPathtoRegion)
{
    DataSet RegionDataSet = new DataSet();
    RegionDataSet.ReadXml(strPathtoRegion);
    dataGridView1.DataSource = RegionDataSet;
}


Any help is appreciated, though I must say I don't think I'm a fan of XML documents.

Edit: Also, I'd like to mention that I cannot change the format of the XML files in anyway as multiple users would use the program and editing the xml files would cause extensive issues later on.

Removed Unnecessary code.
Posted
Updated 2-Jun-11 20:22pm
v4

1 solution

Hi,

I think your code is correct it should work but why you are doing
MIDL
RegionDataSet.Tables.Add(new DataTable("dt"));


DataSet RegionDataSet = new DataSet();
RegionDataSet.ReadXml(strPathtoRegion);
dataGridView1.DataSource = RegionDataSet;
dataGridView1.DataBind();


This should work.

Hope will help you.

Thanks :)
 
Share this answer
 
v2
Comments
Tomwa Ivory 3-Jun-11 1:56am    
Ah that was a left over line from one of the other bits of code I tried, I've tired dozens upon dozens of different methods to fill the table but none have worked I can't understand why.
RakeshMeena 3-Jun-11 1:57am    
Addition of table will not affect the data display since the table is appended at the last and only Dataset's first table data will be shown in the grid by default. Anyways your's is an optimized solution, hence my 5.

Tomwa you probably need to look at your xml file and it's path.
Sanjay J Patolia 3-Jun-11 2:00am    
Thanks :)
Tomwa Ivory 3-Jun-11 2:02am    
the file's path is 100% correct as it is browsed to and multiple checks are made to ensure that the file is present. I can easily read from the XML file with a cmd prompt it's just getting the data to fill a table thats proving problematic. I've had minor success using regular expressions to pull the data from the XML file but that is a painfully long process as there are HUGE numbers of fields to pull data for. And Yes the removal of the unnecessary code is great truthfully I need to do a full sweep of my program as it's starting to get very messy from the various attempts I've made.

Edit also I don't need the DataBind() line as it's not a web based application

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