- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsI've got an XML file that I want to be able to add fields to, edit fields and delete fields.
Below is an example of the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Reports>
<Report>
<Name>Bla bla</Name>
<Location>C:\bla.rpt</Loca
</Report>
<Report>
<Name>boo</Name>
<Location>C:\boo.rpt</Loca
</Report>
</Reports>
I've been able to add, edit and delete with varying degrees of success. (I've been splitting them into seperate functions of add() edit() and delete().
If anyone knows good tutorials on xml files or knows their stuff can you please help?
When adding i'll provide it with two variables: name and location which it then should add to the xml file as seen above:
<Report>
<Name>bla</Name>
<Location>c:\bla.rpt</Loca
</Report>
When editing I pass it what name and location I want to edit and then alters the fields appropriately.
When deleting I want it to delete the <Report></Report> area where the name and location is the same as the passed variables.
I'd give a lot more points for this only i've got just 65 unfortunately.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: steelheart38Posted on 2005-12-05 at 19:48:34ID: 15425213
hi for finding nodes in an XML i recommend learning XPath expression. once you have your node in question, you could access its subnodes, values and even attributes (thus able to edit them). you could also easily delete a node by searching for it using XPath containing the attribute name and value.
ml/default .asp path/defau lt.asp
---------- ---------- ---------- ----------
hString); ---------- ---------- ---------- --------- ---------- ---------- ---------- --------- rt"); ("Name","J ohn Travolta"); ("Location ","Earth") ;
---------- ---------- ---------- ---------
Get enough introduction with XML and XPath using this (though there are more on the web, this is only incase you can't find any)
http://www.w3schools.com/x
http://www.w3schools.com/x
When you have a bit of information on XML and XPath, this is how you integrate xpath in your code
--------------------------
XmlDocument xmlDoc = new XmlDocument();
string filePath = @"<path of your file>";
string xPathString = "<your xpath string>"; // example for xpath string "//report/name='Bla bla'"
xmlDoc.Load(filePath);
XmlNode targetNode;
XmlNode root = xmlDoc.DocumentElement;
targetNode = root.SelectSingleNode(xPat
--------------------------
// when you already have the node you can now manipulate it
// in the case of adding a new report node
--------------------------
XmlElement newReportNode = xmlDoc.CreateElement("Repo
newReportNode.SetAttribute
newReportNode.SetAttribute
root.AppendChild(newNode);
xmlDoc.Save(filePath)
--------------------------
We cannot share to you everything we know about XML and XPath usage in C# because believe me, there's still a lot to learn, but i think this is enough for a start.
hope that helps
Ryan