Solved

Loading and saving the changes of XML data using C#

Posted on 2009-04-04
10
554 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:shanvidhya
10 Comments
 
LVL 9

Accepted Solution

by:
djon2003 earned 55 total points
ID: 24069864
To load & save from/to XML to/from a dataset, uses :
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(FILEPATH);
// Do some modifs
myDataSet.WriteXml(FILEPATH);
0
 
LVL 11

Assisted Solution

by:PlatoConsultant
PlatoConsultant earned 35 total points
ID: 24069867
Binding XML to datagrid and only showing and editing subelements in C#

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/13f5b604-852e-4f28-b8c1-ee531a92681e/

or

use xmldatasource
http://msdn2.microsoft.com/en-us/library/494y92bs(VS.80).aspx
      System.Xml.XmlDataDocument myxml = new System.Xml.XmlDataDocument();
            myxml.DataSet.ReadXml("fullPath\\fileName");
            dataGridView1.DataSource = myxml.DataSet;
            dataGridView1.DataMember = "Product.Product_InfoText";

Open in new window

0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 35 total points
ID: 24070203
the following article show how to bing the xml data to the gridview and then update it
http://www.eggheadcafe.com/articles/20040612.asp

saving the xml has already been mentioned above
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:shanvidhya
ID: 24073381
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  
0
 
LVL 9

Expert Comment

by:djon2003
ID: 24112423
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)
0
 

Author Comment

by:shanvidhya
ID: 24179400
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
0
 
LVL 9

Expert Comment

by:djon2003
ID: 24180179
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.
0
 
LVL 9

Expert Comment

by:djon2003
ID: 24180622
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
0
 

Author Comment

by:shanvidhya
ID: 24180641
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
0
 
LVL 9

Expert Comment

by:djon2003
ID: 24181166
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)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question