[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# Delete from XML

Posted on 2016-08-08
10
Medium Priority
?
76 Views
Last Modified: 2016-08-09
I have an XML with the following structure
<?xml version="1.0" encoding="utf-8"?>
<Reports>
  <personnel>
     <key value="123">abc</key>
     <key value="456">def</key>
  </personnel>
  <configuration>
     <key value="789">ghi</key>
     <key value="012">jkl</key>
  </configuration>
</Reports>

Open in new window

I'm building an interface to allow users to maintain the XML allowing them to delete data from the XML file.  I want them to be able to delete by Key.  

Thanks
0
Comment
Question by:CipherIS
[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
  • 4
  • 4
  • 2
10 Comments
 
LVL 35

Expert Comment

by:ste5an
ID: 41747359
So, what is your concrete question?

What do you mean by "interface"? While it is pretty clear in context with your tags, it makes not so much sense with "users". Do you mean an user interface (UI)?

I would use the .NET built-in XML serialization features. Thus you need a schema of your XML. Then you use the XML Schema Definition Tool (Xsd.exe) to generate the class. Then use XmlSerializer class to load your XML into an object graph. Delete your objects and write it back.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41747375
Yes.  I'm create a GUI to allow the user to look at the values in a grid.  The user will select one of the values from the XML on the Grid then delete it.

Also, this is a winform application.  I'm adding, updating and need to delete data from a file.  How would I serialize the XML?  Examples I've seen for serialization is if I am getting the file from HTTP and streaming it and writing the file.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41747429
Do you already have the XML data loaded into a DataGridView? If so how are you doing this? Will the user need to click a button to actually do the delete?
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.

 
LVL 1

Author Comment

by:CipherIS
ID: 41747437
I will be loading the data onto the gridview.  I'm using infragistics UltraGrid.  The winforms will have a combobox which will contain:  personnel, configuration.

The user will select one of the two (there will be a default selection).  The data will displayed in the grid.  The user will either delete from the grid or a button.  

So, I just need to know how the selected row will delete the XML.

I'm still working on figuring out how to get the delete to work with the UltraGrid.  I found the code with Infragistics working on getting it to fire.  It needs to be added to initialize component.  I opened an issue with this also.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41747481
Hi CipherIS;

Something like this will work.
// Path and file name of the XML
string fileToModify = "C:/Working Directory/reports.xml";
// Load XML into memory
XDocument xdoc = XDocument.Load(fileToModify);
// this value can be retrieved from the UltraGrid control
string keyToDelete = "ghi";

// Find the value in the XML file
var result = xdoc.Root.Descendants("key").Where(v => v.Value == keyToDelete).ToList();
if (result.Count() > 1)
{
    // More then one key was found. Do something to correct the error
    Console.WriteLine("Error more then one key found");
}
else if ( result.Count() != 0 )
{
    // Delete the XML node and write the document back out to disk
    result.ElementAt(0).Remove();
    xdoc.Save(fileToModify);
}
else
{
    Console.WriteLine("Key was not found.");
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 41747504
Thanks.  I will try that.  Also, you have provided me code before.  I have a made modification to it.

Where you have
var result = xdoc.Root.Descendants("key").Where(v => v.Value == keyToDelete).ToList();

Open in new window

I changed to
string element = GetElement(type);
var result = xdoc.Root.Element(element).Descendants("key").Where(v => v.Value == keyToDelete).ToList();

Open in new window

This allows the key in the correct section to be found.  I implemented this when utilizing the update code that you provided me to ensure that I was updating the correct key in the correct section.

Also, comments have been made about serializing.  I've read up on serializing.  I'm adding, updating, and deleting. Can serialization be added for what I'm doing?  I see how it can be added when I'm streaming a file across http but I'm not doing that.

Thanks.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41748154
Hi CipherIS;

As to the changes you made to the code snippet I posted that looks fine. As far as using serialize / deserialize of XML you can do that without sending it over a communications link. When you deserialize an XML you are taking and converting it to a Class object. and when you serialize the Class instance you convert that instance into XML. That XML can be on a disk or retrieved from a network. In this case IMHO it would be over kill.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41748815
Thanks Fernando.  I appreciate the feedback.  I  wasn't too sure and I was researching the heck out of it.  I did find a nice site though.

http://httputility.net/xsd-to-csharp-vb-class.aspx
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41748825
The xsd.exe utitlity is part of Visual Studio and available on the Developer Command prompt.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41748841
Not a problem CipherIS, glad to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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