0

I'm new in the programming world. I'm just looking for help with some kind of code, that would delete node from bunch of xml documents.

It is possible to make something which would delete node in bunch of xml documents at once?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Skippy
  • 3
  • 2
  • 1
    Welcome to Stack Overflow. You'd probably want to write a loop. Each iteration of the loop would load a single document, delete a node, and save the document again. It's hard to give much more help than that at the moment - if you could show what you've tried, we'd be in a better position to give specific help. – Jon Skeet Sep 13 '19 at 15:14
  • To modify a single XML document, you can use LINQ to XML. See e.g. [delete element from xml using LINQ](https://stackoverflow.com/q/13483452), [How does one parse XML files?](https://stackoverflow.com/a/55829/3744182), [LINQ to read XML](https://stackoverflow.com/q/670563/3744182) and [Query an XDocument for elements by name at any depth](https://stackoverflow.com/q/566167/3744182). – dbc Sep 13 '19 at 19:57
  • [How to loop through all the files in a directory in c # .net?](https://stackoverflow.com/q/4254339) might also be relevant. – dbc Sep 13 '19 at 20:04
  • But as it is the question seems too broad as it consists of several independent parts and it's not clear where you're having trouble: 1) Find all XML files. 2) Loop though the files that were found and open them. 3) Parse an XML file. 4) Modify the parsed XML file. 5) Save back the modified file. The rule on stack overflow is to ask [one question per post](https://meta.stackexchange.com/q/222735) so please try to break this down into concrete steps, and show where you are stuck on each. See also: [ask]. – dbc Sep 13 '19 at 20:05

1 Answers1

0

There are many different technologies you could use, which is a bit daunting if you are new to programming. Since this quite a simple task, it's probably not worth investing a lot of time learning new tools: but then it all depends on what you're already comfortable with.

Many people would use XSLT for any job that involves modifying XML documents. You could write an XSLT 3.0 stylesheet transform.xsl like this:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:template match="deleted-node"/>
</xsl:transform>

where "deleted-node" is the name of the nodes you want to delete (or a more complex pattern if you need it). And then you could apply this to all XML files in a directory in, putting the result in directory out, using the Saxon XSLT processor from the command line like this:

Transform -s:in -o:out -xsl:transform.xsl

The way this works is that xsl:mode defines the default processing to be applied to nodes if there isn't a more specific rule; shallow-copy means that you copy the tags and then move on to process the content. There's only one more specific rule, which matches the elements you want to delete; the rule is empty indicating that when you hit one of these elements, you output nothing.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164