使用XDocument和XmlDocument类从XML文件中删除具有特定节点名称的元素

时间:2016-04-06 17:27:09

标签: c# xml

我正在尝试删除具有特定节点名称的元素。使用以下代码但收到错误,如“名称不能以'2'字符开头,十六进制值0x32。”据我所知,这种方法对于相关的xml格式是不正确的。

如何删除具有特定User_Name信息的表。我应该删除特定的表当我尝试删除管理员用户

RemoveElement("Accounts.xml", "User", "Test1");

private static void RemoveElement(string xmlFile, string elementName, string elementAttribute)
    {
        XDocument xDocument = XDocument.Load(xmlFile);
        foreach (var profileElement in xDocument.Descendants("Table").ToList())              
        {
            if (profileElement.Attribute(elementAttribute).Value == elementName) 
            {
                profileElement.Remove();                               
            }
        }
        xDocument.Save(xmlFile);
    }

这是Xml文件;

`<?xml version="1.0" encoding="utf-8"?>
<Accounts>
  <Table>
    <User>Administrator</User>
    <Domain>Localhost</Domain>
    <Password>Test</Password>
    <Account_Type>Windows</Account_Type>
 </Table>
 <Table>
    <User>Test1</User>
    <Domain>demo</Domain>
    <Password>empty</Password>
    <Account_Type>Domain</Account_Type>
 </Table>
</Accounts>`

3 个答案:

答案 0 :(得分:0)

是的,当你从texBox获取信息

时,你可以找到它
xDoc.Load("Accounts.xml");
                foreach (XmlNode node in xDoc.SelectNodes("Accounts/Table"))
                {
                    if (node.SelectSingleNode("User").InnerText == textBox1.Text)
                    {
                        node.ParentNode.RemoveChild(node);
                    }

                }
            xDoc.Save("Accounts.xml");

但我想从listview获取信息。当我尝试使用以下代码时收到错误。

错误:InvalidArgument =值'0'对'index'无效。\ r \ nParameter name:index

listViewMevcutKullaniciListesi.Items.RemoveAt(listViewMevcutKullaniciListesi.SelectedIndices[0]);

xDoc.Load("Accounts.xml");
                foreach (XmlNode node in xDoc.SelectNodes("Accounts/Table"))
                {
                    if (node.SelectSingleNode("User").InnerText == listViewMevcutKullaniciListesi.SelectedIndices[0].ToString())
                    {
                        node.ParentNode.RemoveChild(node);
                    }

                }
            xDoc.Save("Accounts.xml");

答案 1 :(得分:0)

原始代码段不起作用,因为用户名不是用户的属性,而是值。此外,您可以将.Descendants("Table")替换为.Descendants(elementName)以避免不必要的if语句。 我认为,实现所需功能的最优雅方法是使用Linq to Xml:

XDocument xDocument = XDocument.Load(xmlFile);

xDocument.Descendants(elementName)
    .Where(e => e.Value == elementAttribute)
    .ToList()
    .ForEach(e => e.Remove());

xDocument.Save(xmlFile);

至于你的第二个问题:我相信你删除了这一行中的第一个元素

listViewMevcutKullaniciListesi.Items.RemoveAt(listViewMevcutKullaniciListesi.SelectedIndices[0]);

当您第二次致电listViewMevcutKullaniciListesi.SelectedIndices[0]时,您显然会遇到异常。 此外,listViewMevcutKullaniciListesi.SelectedIndices[0].ToString()不会为您提供所选项目,只会提供其编号。

答案 2 :(得分:0)

以下是XmlDocument类的答案; 你正在调用像

这样的方法
`RemoveElementWithXmlDocument("Accounts.xml", "Accounts/Table", "User", listViewMevcutKullaniciListesi.SelectedItems[0].Text);`

方法

private static void RemoveElementWithXmlDocument(string xmlFile, string nodeName, string elementName, string elementAttribute)
    {
        xDoc.Load(xmlFile);
        try
        {
            foreach (XmlNode node in xDoc.SelectNodes(nodeName))
            {
                if (node.SelectSingleNode(elementName).InnerText == elementAttribute)
                {
                    node.ParentNode.RemoveChild(node);
                }
            }
            xDoc.Save(xmlFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

而且我也希望将XDocument类用于相同的结构但是我收到一个异常,例如&#34;对象引用未设置为对象的实例&#34;在foreach循环中运行profileElement.Remove();线。如果我注释掉这一行永远不会得到异常,但我需要这一行来从xml中删除相关节点。所以,据我所知,我在XDocument中遗漏了一些东西。需要你的帮助

RemoveElementWithXDocument("Accounts.xml", "Table", "User", listViewMevcutKullaniciListesi.SelectedItems[0].Text);

XDocument的方法

private static void RemoveElementWithXDocument(string xmlFile, string nodeName, string elementName, string elementAttribute)
    {
        XDocument xDocument = XDocument.Load(xmlFile);
        try
        {
            foreach (XElement profileElement in xDocument.Descendants(nodeName))
            {
                if (profileElement.Element(elementName).Value == elementAttribute)
                {
                    profileElement.Remove();
                }
            }
            xDocument.Save(xmlFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }