简体   繁体   中英

Remove selected datagridview element from xml file

I have an xml file(data/tools.xml) created by C# program like this:

<?xml version="1.0"?>
<ArrayOfToolClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ToolClass>
    <ToolID>1</ToolID>
    <ToolName>Multiflute Endmill</ToolName>
    <ToolDia>8</ToolDia>
    <ToolTooth>4</ToolTooth>
    <ToolApmxs>32</ToolApmxs>
    <ToolCuttingSpeed>200</ToolCuttingSpeed>
    <ToolFeedPerTooth>0.05</ToolFeedPerTooth>
    <ToolAe>4</ToolAe>
    <ToolAp>8</ToolAp>
    <ToolManufacturer>SECO</ToolManufacturer>
    <ToolSerial>FHAKJSH</ToolSerial>
  </ToolClass>
  <ToolClass>
    <ToolID>2</ToolID>
    <ToolName>Multiflute Endmill</ToolName>
    <ToolDia>4</ToolDia>
    <ToolTooth>4</ToolTooth>
    <ToolApmxs>25</ToolApmxs>
    <ToolCuttingSpeed>200</ToolCuttingSpeed>
    <ToolFeedPerTooth>0.03</ToolFeedPerTooth>
    <ToolAe>2</ToolAe>
    <ToolAp>4</ToolAp>
    <ToolManufacturer>SECO</ToolManufacturer>
    <ToolSerial>SJKHDKJ</ToolSerial>
  </ToolClass>
  <ToolClass>
    <ToolID>3</ToolID>
    <ToolName>Multiflute Endmill</ToolName>
    <ToolDia>20</ToolDia>
    <ToolTooth>4</ToolTooth>
    <ToolApmxs>38</ToolApmxs>
    <ToolCuttingSpeed>45</ToolCuttingSpeed>
    <ToolFeedPerTooth>0.02</ToolFeedPerTooth>
    <ToolAe>2</ToolAe>
    <ToolAp>20</ToolAp>
    <ToolManufacturer>SECO</ToolManufacturer>
    <ToolSerial>SJKHDKJ</ToolSerial>
  </ToolClass>
</ArrayOfToolClass>

I loaded this file in windows form application to datagridview. Program image 1 I want to delete the item from xml file that was selected in datagridview. I tried with this code:

 private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Biztosan törlöd a szerszámot?", "Törlés", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            int id = (ToolsDataGridView.SelectedRows[0].Index) + 1;
            Console.WriteLine(id);

            XDocument xmlDoc = XDocument.Load(@"data\tools.xml");
            var elementsToDelete = from ele in xmlDoc.Descendants("ToolClass")
                                   where ele != null && ele.Element("ToolID").Value == id.ToString()
                                   select ele;

            foreach (var x in elementsToDelete)
            {
                x.RemoveAll();


            }

            xmlDoc.Save(@"data\tools.xml");
            ReadXmlData();
        }

The code remove the selected element from xml but place an <ToolClass /> to its place. Can somebody help me, what do I do wrong?

Another problem with the code: When I deleted an element, the ToolID do not match with the datagridview selected row index. How can I get the correct ToolID based on the selected line?

Thanks!

A data grid binds to its data source. In a simple example of a list of elements, making changes in the grid such as ticking a tick box will update the underlying data that it still references.

I don't think it is possible to perform the same manipulation using and XML data source.

As long as your code correctly removes the required XML elements, you are likely to have to rebind the file to the grid again - I'm not sure if that is what your line "ReadXmlData();"does.

To get a value from a cell such as an ID, you need to use ToolsDataGridView.SelectedRows[0].Cells[...] - either by name or position, then get the value which you cast to the correct type - in your case it will be a string and not an integer since the data is coming from XML with no schema or other definition to call on

Couple of points -

  1. The code remove the selected element from xml but place an to its place. Can somebody help me, what do I do wrong?

There is nothing wrong with the code and that is the expected behavior when you delete the elements from the XML Array. If you really want to remove these empty elements then remove them before saving the XML. In your case below code will help:

 xmlDoc.Descendants().Where(d => d.IsEmpty || String.IsNullOrWhiteSpace(d.Value)).Remove();

Here you can read more on removing empty elements.

  1. When I deleted an element, the ToolID do not match with the datagridview selected row index. How can I get the correct ToolID based on the selected line?

The code you have will work fine for removing the records if the selected ToolID is in the form of incremental way, For example in your sample XML the ToolIDs are 1,2 and 3 and if you select a 1st row then it will delete that row as you are adding 1 to selected index, Else if your ToolIDs are same similar to the Data in screenshot(Like similar ToolIDs for multiple records) then you have to changes the below logic which will delete all the records with that ToolID-

int id = (ToolsDataGridView.SelectedRows[0].Index) + 1;

To,

int id = Convert.ToInt32((dataGridView1.SelectedRows[0].Cells["ToolID"]).Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM