The simplest and probably fastest way might be to do a simple file based replace, you can read a file into a
List<string>
with
File.ReadAllLines()
.
Then loop through all lines and replace the <credits> lines by deleting them and inserting at the new position.
Another way would be to use the
XElement
class, see:
XElement Class (System.Xml.Linq) | Microsoft Docs[
^]
and:
Serializing XML Trees (C#) | Microsoft Docs[
^]
This should get you started:
test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<image>
<caption>This is a caption.</caption>
<credits>This is a test sentence.</credits>
</image>
<image>
<caption>This is a caption 2.</caption>
<credits>This is a test sentence 2.</credits>
</image>
</root>
using System.Xml.Linq;
using System.Collections.Generic;
string fileName = @"test2.xml";
var xmlSource = XElement.Load(fileName);
var xList = xmlSource.Elements("image");
foreach (var x in xList)
{
if (x == null)
{
continue;
}
var credits = x.Element("credits");
if (credits != null)
{
Debug.Print(credits?.Value);
var c = credits;
c.Remove();
}
}
xmlSource.Save(fileName + "new");