I currently have a XML document built using the following structure:
<a>
....
<b>
<c>
</c>
<d>
....
</d>
</b>
</a>
<a>
....
I would like to parse this XML document using C# and output a document in which all the b-nodes are emptied, without losing my b-node. Thus creating the following result:
<a>
...
<b />
</a>
<a>
...
Can anyone show me the way to do this?
LINQ to XML would make this pretty simple:
var doc = XDocument.Load(...);
var bs = doc.Descendants("b").ToList();
foreach (var b in bs)
{
b.ReplaceNodes();
}
(Use ReplaceAll
instead of ReplaceNodes
if you want to remove the attributes within b
nodes as well.)
See more on this question at Stackoverflow