Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:24:28+00:00 2026-06-04T19:24:28+00:00

I use an XML file to store from and display the contents on a

  • 0

I use an XML file to store from and display the contents on a ListBox.

Here is an sample XML file;

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Entry>
<Details>0</Details>
</Entry>
<Entry>
<Details>1</Details>
</Entry>
<Entry>
<Details>2</Details>
</Entry>
<Entry>
<Details>3</Details>
</Entry>
<Entry>
<Details>4</Details>
</Entry>
<Entry>
<Details>5</Details>
</Entry>
<Entry>
<Details>6</Details>
</Entry>
</Root>

Users can select the values on the ListBox (selection mode is MultiExtended) and delete them.

My problem is, well, it’s better to show than to explain;

Selected Items —

Selected Items

After Pressing the Del key —

Deleted Items

Contents of the XML file are same as of ListBox.

The result is even more weird when I select all and press delete.

Am I doing something wrong ?

How to get the indexes of multiple items and handle them correctly ?

Here’s my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

namespace XML_ListBox
{
    public partial class Form1 : Form
    {
        string path = "Test.xml";
        public Form1()
        {
            InitializeComponent();
            LoadFile();
        }

        private void LoadFile()
        {
            XDocument xdoc = XDocument.Load(path);
            foreach (var el in xdoc.Root.Elements())
            {
                listBox1.Items.Add(el.Element("Details").Value);
            }
        }

        private void OnDelete(object sender, KeyEventArgs e)
        {
            XElement root = XElement.Load(path);

            if (e.KeyCode == Keys.Delete)
            {
                foreach (Object index in listBox1.SelectedIndices)
                {
                    root.Elements("Entry").ElementAt((int)index).Remove();
                    listBox1.Items.RemoveAt((int)index);
                }

                root.Save(path);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:24:31+00:00Added an answer on June 4, 2026 at 7:24 pm

    Your code trys to delete items by indexes, but each time wnen you delete an item with the index X, the item with index X+1 will be moved to the index X.
    So each time when you delete the item with Index = 0 the item with Index 5 become to the Index 4.

    You can try to sort indexes:

    if (e.KeyCode == Keys.Delete)
    {
      foreach (int index in listBox1.SelectedIndices.Cast<int>().OrderByDescending(i=>i))
      {
        root.Elements("Entry").ElementAt(index).Remove();
        listBox1.Items.RemoveAt(index);
      }
    
      root.Save(path);
    }
    

    But the preffered way to delete items is delete by Key value instead of the index value

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this XML file here. <?xml version=1.0 encoding=UTF-8?> <xml> <word> <threeletter>RIP</threeletter> <threeletter>PIE</threeletter> <fourletter>PIER</fourletter>
I am trying to use an XML file to store my high score, here
Imagine you have a data.xml file: <?xml version=1.0 encoding=UTF-8 ?> <root> <item>value1</item> <item>value2</item> <item>value3</item>
I need to download xml file from a secure link, and store the content
I was initially going to use a signed serialized xml file to store license
I try to use LINQ to read XML file. However, the string which store
I'm having trouble understanding why I would use a context.xml file to declare a
I use a xsl tranform to convert a xml file to html in dotNet.
I have this use case of an xml file with input like Input: <abc
I want to use interface instead of mapper XML file in MyBatis. In the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.