0

I have a file like this

<?xml version="1.0"?>
<DD:node1 xmlns:DD="www.dd.com/dd"
  DD:attr1="1"
  DD:attr2="2"
  <DD:Props
    DD:Name="anyName">123</DD:Props>
  <DD:node2>
    <DD:node21>
      DD:attr1="1"
      DD:attr2="2"
    </DD:node21>
    <DD:node22>
      DD:attr1="1"
      DD:attr2="2"
    </DD:node22>
  </DD:node2>
  <DD:node3>
    <DD:node31>
      DD:attr1="1"
      DD:attr2="2"
    </DD:node31>
    <DD:node32>
      DD:attr1="1"
      DD:attr2="2"
    </DD:node32>
  </DD:node3>
</DD:node1>

What is the best way to split this file to few as every node to another file, but with some rules:

  1. Some files should contain only attributes of node and its name, without child nodes
  2. Some files should contain child nodes entirely (which nodes in which files, entirely or not I will describe in my code)
  3. Files should be write to different folders, dependence of some attributes values or nodes names
  4. General condition: to save tags "as is" in original file - without xmlns section in every file, but with prefix of namespace. I know that in context of xml this will be a bad files, but it does not matter in my case.

Expected result:

First file

<?xml version="1.0"?>
<DD:node1 xmlns:DD="www.dd.com/dd"
DD:attr1="1"
DD:attr2="2"
</DD:node1>

Second file

<DD:Props
DD:Name="anyName">123</DD:Props>

third

<DD:node21>
  DD:attr1="1"
  DD:attr2="2"
</DD:node21>

and no file with

<DD:node2> </DD:node2>

only subchild of node2 (it does not metter node2 or node3 in this case, this is just an example)

General condition: to save tags "as is" in original file - without xmlns section, but with prefix of namespace.

as if you work not with xml, but with the simple text file and splitting it

I dont ask how to write files in folders, I'm looking for how to split with format saving

TJS
  • 81
  • 5
  • Why do you want to create a lot of files? Wouldn't it be better to parse into a single file. I prefer to create a DataTable. See this treeview solution : https://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Aug 12 '19 at 15:09
  • 1
    Your requirement to create XML that is not namespace-well-formed (a) restricts your options for reading those files, because most XML tools can only read namespace-well-formed XML, and (b) restricts your choice of technology for doing the splitting: XSLT, for example, will always produce namespace-well-formed output. It looks like you are trying to shoot yourself in the foot and having trouble finding a suitable gun. – Michael Kay Aug 12 '19 at 15:38
  • @MichaelKay, Thank you for worrying about me ))) I understand this files will be corrupted, but there is no plan to read it programmatically. It's just for comfort "eyes view" for node difference and no more. I already have gun, just show me direction where to shoot ) – TJS Aug 12 '19 at 18:12
  • @jdweng Lot of files give me possibility to see how many nodes has a difference, when I compare them with file comparision tool. Single file does not allow that without advanced comparision tool. – TJS Aug 12 '19 at 18:23
  • Only if you name the files so you can compare. If you have a 1000 files named 1,2,3,4,5,6 which files do you compare. You will get better statistics if you group the node in c# and display. – jdweng Aug 12 '19 at 18:45
  • Of course, my files will be named as node name or some attribute value to convey the meaning of file contents. And of course nodes has normal names, not node1, node2. – TJS Aug 12 '19 at 19:04
  • If you're prepared to output files that either omit the namespace prefix or declare it, then the problem is easy to solve using XSLT 2.0 (or 3.0). I can't see why you don't want to do that. – Michael Kay Aug 12 '19 at 19:58
  • @MichaelKay Just because I never used XSLT, please don't hurling stones at me :( I'll try it for sure! Thanks for you pointing. – TJS Aug 12 '19 at 20:19

0 Answers0