簡體   English   中英

使用XDocument和XmlDocument類從XML文件中刪除具有特定節點名稱的元素

[英]Delete an element with specific node name from XML file using XDocument and XmlDocument class

我正在嘗試刪除具有特定節點名稱的元素。 使用以下代碼,但會收到類似“名稱不能以'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>`

是的,發現從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”對“索引”無效。\\ r \\ n參數名稱:索引

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");

原始代碼段無效,因為用戶名不是用戶的屬性而是值。 另外,您可以將.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()不會給您選定的項目,而只是給您編號。

這是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類,但是當運行profileElement.Remove();時,我在foreach循環中收到一個類似“對象引用未設置為對象實例”的異常。 線。 如果我對此行進行注釋,則永遠不會出現異常,但是我需要此行來從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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM