Deleting an element from XML document and saving over old file

限于喜欢 提交于 2019-12-12 05:21:50

问题


*EDIT: ok, so I got the program to delete, the line foreach (var elem in doc.Document.Descendants("Profiles")) needed "Profile" instead. BUT now in my XML doc there is an empty Profile Element for each one deleted, so If all the items in the xml example(at bottom of question) get deleted I'm left with this:*

<?xml version="1.0" encoding="utf-8"?>
<Profiles>
  <Profile />
  <Profile />
</Profiles>

=========================ORIGINAL QUESTION BELOW=================================

I'm using the following code to remove a element and it's children from an XML file, BUT it isn't removing them from the file upon saving. Could someone let me know why this is incorrect?

    public void DeleteProfile()
    {
        var doc = XDocument.Load(ProfileFile);
        foreach (var elem in doc.Document.Descendants("Profiles"))
        {
            foreach (var attr in elem.Attributes("Name"))
            {
                if (attr.Value.Equals(this.Name))
                    elem.RemoveAll();
            }
        }
        doc.Save(ProfileFile,
        MessageBox.Show("Deleted Successfully");
    }

EDIT: an example of the XML format below

<?xml version="1.0" encoding="utf-8"?>
<Profiles>
  <Profile Name="MyTool">
    <ToolName>PC00121</ToolName>
    <SaveLocation>C:\Users\13\Desktop\TestFolder1</SaveLocation>
    <Collections>True.True.True</Collections>
  </Profile>
  <Profile Name="TestProfile">
    <ToolName>PC10222</ToolName>
    <SaveLocation>C:\Users\14\Desktop\TestFolder2</SaveLocation>
    <Collections>True.False.False</Collections>
  </Profile>
</Profiles>

回答1:


I assume you want to remove a profile given its name:

private static void RemoveProfile(string profileFile, string profileName)
{
    XDocument xDocument = XDocument.Load(profileFile);
    foreach (var profileElement in xDocument.Descendants("Profile")  // Iterates through the collection of "Profile" elements
                                            .ToList())               // Copies the list (it's needed because we modify it in the foreach (when the element is removed)
    {
        if (profileElement.Attribute("Name").Value == profileName)   // Checks the name of the profile
        {
            profileElement.Remove();                                 // Removes the element
        }
    }
    xDocument.Save(profileFile);
}

If you have only an empty element is because you use RemoveAll() (removes the descendants and attributes of an element) instead of Remove() (removes the element it self from its parent).

You can even remove the if by replacing it by a where in the LINQ query:

foreach (var profileElement in (from profileElement in xDocument.Descendants("Profile")      // Iterates through the collection of "Profile" elements
                                where profileElement.Attribute("Name").Value == profileName  // Checks the name of the profile
                                select profileElement).ToList())                             // Copies the list (it's needed because we modify it in the foreach (when the element is removed)
    profileElement.Remove();  // Removes the element

xDocument.Save(profileFile);



回答2:


....
foreach (var elem in doc.Document.Descendants("Profiles"))
{
    foreach (var attr in elem.Attributes("Name"))
    {
           if (attr.Value.Equals(this.Name))
               TempElem = elem;
    }
}
TempElem.Remove();
...

I'm dumb lol, this solves everything



来源:https://stackoverflow.com/questions/17840688/deleting-an-element-from-xml-document-and-saving-over-old-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!