Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从XML文件中删除条目?_C#_Xml_Listbox - Fatal编程技术网

C# 如何从XML文件中删除条目?

C# 如何从XML文件中删除条目?,c#,xml,listbox,C#,Xml,Listbox,我将xml信息填充到一个列表框(LST)中。我可以使用以下代码从列表框中删除: private void btnAdopt_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Complete Adoption?", "Found a Happy Home!", MessageBoxButtons.YesNo); if (result == DialogRe

我将xml信息填充到一个列表框(LST)中。我可以使用以下代码从列表框中删除:

 private void btnAdopt_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("Complete Adoption?", "Found a Happy Home!", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {

            if (lstAnimals.SelectedIndex >= 0)
                lstAnimals.Items.Remove(lstAnimals.SelectedItem);                                                        

        }
        else
        {
            return;
        }
但是,当列表更新/程序打开时,该条目将重新填充到列表框中。如何与lixtBox同时从XML文档中删除它

以下是xml:

<?xml version="1.0" encoding="UTF-8"?>
 <Animals>
  <Animal>
   <Name>Bruce</Name>
   <Type>Dog</Type>
   <Age>Adult</Age>
  </Animal>
  <Animal>
   <Name>Gizmo</Name>
   <Type>Cat</Type>
   <Age>Senior</Age>
  </Animal>
 </Animals>


您可以使用与此类似的XmlDocument removeChild方法

XmlDocument doc ;

doc = new XmlDocument(); 
doc.Load("path to your XML file");

XmlNode animalNode;
XmlNode root = doc.DocumentElement;

animalNode=root.SelectSingleNode("descendant::Animal[Name='" + lstAnimals.SelectedItem + "']");
doc.RemoveChild(animalNode) ;

//save the XML file

将此代码添加到按钮单击事件中

        string text = lstAnimals.SelectedItem.ToString();
        string animalName = text.Substring(0, text.IndexOf("is")).Trim();           
        XDocument xDoc = XDocument.Load("Animals.xml"); //here is your filepath
        XElement element = (from x in xDoc.Descendants("Animal")
            where x.Element("Name").Value == animalName  
            select x).First();
        element.Remove();
        xDoc.Save("Animals.xml");
        lstAnimals.Items.Remove(lstAnimals.SelectedItem);

当然,若你们把Id属性添加到你们的动物身上,那个就更容易了。只要把Id变量存储到你们的listBox项“Tag”属性中就行了

这里是如何使用linq to xml实现的

     private void btnAdopt_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("Complete Adoption?", "Found a Happy Home!", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {

            if (lstAnimals.SelectedIndex >= 0)
{
                lstAnimals.Items.Remove(lstAnimals.SelectedItem);  
XDocument xDoc = XDocument.Load("test.xml");
            xDoc.Descendants("Animal").Where(x => x.Element("Name").Value.ToString() == lstAnimals.SelectedItem ).Remove();
            xDoc.Save("test1.xml"); 

}                                                      

        }
        else
        {
            return;
        }

使用列表框显示您的代码。它的填充方式和删除/更新您可能将xml反序列化到您的对象中。您必须再次序列化它并保存旧的。我使用填充列表框的代码进行了编辑。我似乎无法使其正常工作。它仍保留xml文档中的条目。Visual studio不允许我在.selected项之后添加.Text或.SubItems。我可能缺少using语句吗?我有一个字符串,其内容为“name”是一个“type”,在列表框中是一个(n)“age”。我将尝试您的编辑。尝试了(如果(lstAnimals.SelectedIndex>=0)并得到以下异常:“对象引用未设置为对象的实例”。您可以共享您的列表框照片吗?这是指向图片的链接:
     private void btnAdopt_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("Complete Adoption?", "Found a Happy Home!", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {

            if (lstAnimals.SelectedIndex >= 0)
{
                lstAnimals.Items.Remove(lstAnimals.SelectedItem);  
XDocument xDoc = XDocument.Load("test.xml");
            xDoc.Descendants("Animal").Where(x => x.Element("Name").Value.ToString() == lstAnimals.SelectedItem ).Remove();
            xDoc.Save("test1.xml"); 

}                                                      

        }
        else
        {
            return;
        }