?
Solved

Loading and saving the changes of XML data using C#

Posted on 2009-04-04
10
Medium Priority
?
593 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 9

Accepted Solution

by:
djon2003 earned 165 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 105 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 105 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 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