Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading and saving the changes of XML data using C#

Posted on 2009-04-04
10
Medium Priority
?
606 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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