Medic3000
Medic3000

Reputation: 786

Deleting an element from XML document and saving over old file

*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>

Upvotes: 0

Views: 11539

Answers (2)

Medic3000
Medic3000

Reputation: 786

....
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

Upvotes: 0

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

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

Upvotes: 3

Related Questions