Link to home
Start Free TrialLog in
Avatar of shanvidhya
shanvidhya

asked on

Loading and saving the changes of XML data using C#

Hello C# gurus; I am new to C#; I want to load an XML file and display the data in an Grid based on certain conditions. After loading data I wanted to make some changes to the data in grid and then wanted to save the changes back to XML file. Please let me know how can I do that. Please provide any sample code. Thanks for your help in advance.
ASKER CERTIFIED SOLUTION
Avatar of djon2003
djon2003
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of PlatoConsultant
PlatoConsultant
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shanvidhya
shanvidhya

ASKER

I have following values in XML:

Group   Color      Value
-------------------------------------
1          Red         25
1         Yellow      30
2         Orange      50
2         Purple        100
2        Red            25

I wanted to display only unique group in a dropdown from the XML then based on the group seletion display the appropriate values in the grid. If someone add/modify the values for that group those values need to be stored in a separate XML. This is the full description of the requirement and not just open and save to XML.

Thanks,
Shan  
Well looping through the dataset resulting of the command I gave you first, gives you the possibility to build an ArrayList of the groups.

Then you set the datatable 0 of the dataset to a DataGridView.

Then you set this arraylist in the items of a ComboBox, which on the SelectedIndexChanged event you set the DataGridView.RowFilter = 'Group=' + ComboBox.SelectedItem.ToString;

When the user finish the modifications, you save the DataSet to the XML file of your choice with the command above. (First post)
Folloing is the XML I have:

<GSMThreshold>
<BasicMetrics>
<MetricCategory>
 GSM Pilot
<MetricName>
 GSM Dominant Pilot EcIo
<Rec1>
 <Threshold>-11</Threshold>
 <Color>16711935</Color>
 </Rec1>
<Rec2>
 <Threshold>-9</Threshold>
 <Color>16711935</Color>
 </Rec2>
 </MetricName>
<MetricName>
 GSM Pilot Scan Ec
<Rec1>
 <Threshold>-80</Threshold>
 <Color>16711935</Color>
 </Rec1>
<Rec2>
 <Threshold>-75</Threshold>
 <Color>16711935</Color>
 </Rec2>
 </MetricName>
 </MetricCategory>
<MetricCategory>
 GSM Power
<MetricName>
 GSM Best Neighbor Rx Level
<Rec1>
 <Threshold>-120</Threshold>
 <Color>16711935</Color>
 </Rec1>
<Rec2>
 <Threshold>-105</Threshold>
 <Color>16744703</Color>
 </Rec2>
 </MetricName>
<MetricName>
 GSM Serving Cell Rx Level Full
<Rec1>
 <Threshold>-120</Threshold>
 <Color>16711935</Color>
 </Rec1>
<Rec2>
 <Threshold>-105</Threshold>
 <Color>16744703</Color>
 </Rec2>
 </MetricName>
<MetricName>
 GSM Serving Cell Rx Level Sub
<Rec1>
 <Threshold>-120</Threshold>
 <Color>16711935</Color>
 </Rec1>
<Rec2>
 <Threshold>-105</Threshold>
 <Color>16744703</Color>
 </Rec2>
 </MetricName>
 </MetricCategory>
 </BasicMetrics>
 </GSMThreshold>

The hierarchy is
BasicMetrics->MetricCategory->MetricName->RecX->
                                                               Threshold
                                                                Color

I want to display each hierarchy level in a separate listbox; so that:
1.When I select  BasicMetrics -> It should display all metric category
2. When I select Metric Category -> it should list all metricname
3. When MetricName selected -> list all records

Can someone advice how to search for some nodes in XML? Say If I select "GSM Pilot" I want to get the node vales for "MetricName" which is suppose to be ("GSM Dominant Pilot EcIo" and " GSM Pilot Scan Ec")

Thanks,
SHan
Excuse me, but I will stop here. I consider that you changed of question. First was loading & saving XML from/to a DataGridView. Now, it seems that your XML is not containing only one table, but more than one. So you are looking how to load that up.

I never use this kind of XML (well not that way). I would suggest loading the XML into an XMLDocument as suggested by PlatoConsultant above. When loaded in an Object, you can explore the document and use it as you need. Let say populating a combobox with one level of your XML.

If you search for someone giving the answer as if you had pay for it, forget this. We are here to guide you through your problem, not necessarily solving the whole problem. I think that learning by ourselves is the way to learn for a long term.

Google on XMLDocument and XML with C#. After then, come back here when you will be stuck and you will be able to pin point your problem. Not : "Well, how do I build this or that.." This is not a good question, because you are asking us to do it for you. No way ! Please inform yourself on XML, then come back.

Excuse me if I'm a little bit direct, but I'm tired of those who try to make their work done by others.
Here.. browsing the web I found someone who have wrote an example in VB.Net (easy to transfer to C#) of XML and two other namespaces.

You should be able to learn from there :
http://www.vb-helper.com/handy_namespaces.zip
djon,

I am sorry if this made you little frustating. In the question I have mentioned loading data based on certain conditions. I should have given full details earlier. As I mentioned I am new to C# and thought of getting guidance for this type of complex situations. I am looking for ideas only and not asking for exact programs.

Thanks,
Shan
You should have all the information needed within my comments and looking at the examples I gave you (looking here can mean digging by googling what you don't understand)