Solved

Loading and saving the changes of XML data using C#

Posted on 2009-04-04
10
517 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:shanvidhya
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:shanvidhya
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now