this is a question about this thread: http://www.daniweb.com/forums/thread331447-3.html
there is an option to delete item from the XML FILE?

because i have 'Delete item button from the listView' and i want that will be delete it also from the XML file..

and if there's no way to do that. maybe there is possible way? like to delete the XML file and then to make it again? like in the program:

1) i write something
2) i close the program
3) i open the program and i see the items in the listView
4) i delete items as i want.
5) i close the program
6) i open the program again and i see the changes// (if i delete 3/6 items i will see the rest items(3 items)

i would like you to do that on my code: http://www.mediafire.com/?pk27euc5r8fndeq

thanks!

Look at this sample and implement it in your code.

using System;
using System.Xml;
using System.Linq;
using System.Xml.Linq;

class Program
{
    public static void Main(string[] args)
    {
        string path = @"c:\folder\sample.xml";

        XDocument doc = XDocument.Load(path);

        foreach (var result in doc.Descendants("No"))
        {
            if (result.Value == "10")
            {
               result.Parent.Remove();
               break;
            }
            
        }
        doc.Save(path);
    }
}

sample.xml

<?xml version="1.0" encoding="utf-8"?>
<Records>
  <Record>
    <No>20</No>
    <Name>B</Name>
  </Record>
  <Record>
    <No>30</No>
    <Name>C</Name>
  </Record>
</Records>

Edited 5 Years Ago by __avd: n/a

can you do that on my code please? i don't know how because i have just learned this.
now i know how to add XML file perfectly but i don't know how to delete item.
so you can do that on my code that i gave before? i will appreciate it.

you just aren't removing the item from the original collection.

this is what you need to do, modify the code that adds the new items to the listview to store a reference to the myObject instance in its Tag property. then on your code that removes the item from the istview, first remove the item stored in its tag from your List<myObject> collection.

Its really very simple. I don't undertand why you are having a problem. I am going to show you some example code, this code is NOT just paste in code for your application. I am just going to show you HOW its done with a different example.

//this method will add new item to both your listview and the datalist that holds the data to be saved
        public void addItem(ListView listViewToAddTo, List<someObject> listThatHoldsData, string Col1, string Col2)
        {
            //create the listview item to display it in the listview
            ListViewItem newItem = new ListViewItem(Col1);
            newItem.SubItems.Add(Col2);

            //create the dataobject that holds the information
            someObject newSomeObject = new someObject();
            newSomeObject.Column1 = Col1;
            newSomeObject.Column2 = Col2;

            //add the data object to the main list
            listThatHoldsData.Add(newSomeObject);

            //set the data object reference to the tag object of the lsitview item that way we can find it easy
            newItem.Tag = newSomeObject;

            //finally add that item to the listview
            listViewToAddTo.Items.Add(newItem);

        }

        public void removeItem(ListView listViewToRemoveFrom, List<someObject> listThatHoldsData)
        {
            //get the selected item
            ListViewItem selecteditem = listViewToRemoveFrom.SelectedItems[0];

            //get the original object, stored as a reference in the selecteditem's tag
            someObject selectedObject = (someObject)selecteditem.Tag;

            //remove the item from the original list
            listThatHoldsData.Remove(selectedObject);

            //finaly remove the selected item from the listview
            listViewToRemoveFrom.Items.Remove(selecteditem);
        }

    
    //this is our example object
    public class someObject
    {
        public string Column1 { get; set; }
        public string Column2 { get; set; }
    }

If you learned from my example and implemented all the changes I explained. it would certainly work.

The problem is that you aren't passing the changes (in this case deleteing) from the listview back to the collection that holds your data.

At this point I have helped you all I am willing, If you want someone to write your program for you. Tell me all what you need and pay me to do it. I love to help out the community here at daniweb and I'll teach you to fish, but I won't catch your fish for free.

please Dimonddrake you the only one that can help me with that.
i will really appreciate this

I posted an explanation. ALL you have to do is when you remove the item from the listview, find that item in the generic list of objects and remove it there. I don't understand what you don't get.

i have been trying to do something like that but it doesn't work:

XmlDocument ListViewItemsXml = new XmlDocument();
            ListViewItemsXml.Load(ListViewItems);
            XmlElement root = ListViewItemsXml.DocumentElement;
            foreach (XmlElement x in root.ChildNodes)
            {
                if (i don't know what to do)
                {
                    root.RemoveChild(x);
                    break;
                }
            }

            ListViewItemsXml.Save(ListViewItems);

Edited 5 Years Ago by IDC_Sharp: n/a

if you are still using the serialization class that I provided for you. then you are doing it all wrong. Don't worry about the xml. its created automatically. Just edit the generic list.

in this code example http://www.daniweb.com/forums/post1420401.html#post1420401

I explained the changes that you needed to make, why have you not followed it? It is not paste in code, but it explains what you need to do.

you have a list that HOLDS all your data, then you have a listview that DISPLAYS all your data. when you remove the data from the LISTVIEW you need to also remove it from the LIST that holds the data.

the easiest way would be when you add the data to the listview that you set the listview item's tag property to a reference tot he object in the original list that way you can remove the data from the list using the listitems tag property.

Just try to do what I am telling you. If after 2 hours, you still don't get it. Post your entire project as it is and I will edit it to work.

I really hate to see programmers give up like this.

i tryed some stuff but always i have problem with like 1 line that all and when it doesn't show an error it doesn't do it right.

This article has been dead for over six months. Start a new discussion instead.