C# 从XML文档中删除元素并保存到旧文件
*编辑:好的,所以我得到了要删除的程序,C# 从XML文档中删除元素并保存到旧文件,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,*编辑:好的,所以我得到了要删除的程序,foreach行(doc.Document.subjections(“Profiles”)中的var elem)需要“Profile”。但是现在在我的XML文档中,每个删除的项目都有一个空的Profile元素,所以如果XML示例中的所有项目(在问题的底部)都被删除了,我就剩下这个: 编辑:下面的XML格式示例 <?xml version="1.0" encoding="utf-8"?> <Profiles> <Profil
foreach行(doc.Document.subjections(“Profiles”)中的var elem)
需要“Profile”。但是现在在我的XML文档中,每个删除的项目都有一个空的Profile元素,所以如果XML示例中的所有项目(在问题的底部)都被删除了,我就剩下这个:
编辑:下面的XML格式示例
<?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>
PC00121
C:\Users\13\Desktop\TestFolder1
真的,真的,真的
PC10222
C:\Users\14\Desktop\TestFolder2
对,错,错
我假设您要删除一个名为的配置文件:
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);
}
如果只有空元素,是因为使用了RemoveAll()
(删除元素的子体和属性)而不是Remove()
(从父元素中删除元素本身)
您甚至可以在LINQ查询中将if
替换为where
,从而删除:
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);
我假设您要删除一个名为的配置文件:
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);
}
如果只有空元素,是因为使用了RemoveAll()
(删除元素的子体和属性)而不是Remove()
(从父元素中删除元素本身)
您甚至可以在LINQ查询中将if
替换为where
,从而删除:
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);
我是傻瓜,哈哈,这解决了一切
我是傻瓜哈哈,这解决了所有问题你试过调试它并确保首先删除了元素吗?也许没有删除任何内容,而您只是再次保存了相同的文件?这就是我认为正在发生的事情,我只是对XML不够熟悉,不知道是否正确地进行了删除。我将再次进行调试,看看能从中推断出什么,但我也将抛出一个问题中的xml示例,以防社区清楚这里出了什么问题。您是否尝试过调试它并确保首先删除了元素?也许没有删除任何内容,而您只是再次保存了相同的文件?这就是我认为正在发生的事情,我只是对XML不够熟悉,不知道是否正确地进行了删除。我会再次调试,看看我能推断出什么,但我也会抛出一个问题中的xml示例,以防社区清楚这里出了什么问题。我解决了这个问题,但你的答案稍微干净和优雅,所以我会继续选择这个。我解决了这个问题,但你的答案稍微干净和优雅,所以我会继续选择这个
....
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();
...