0

I know that pretty print questions have been asked countless times, but i haven't found any information to solve my problem.

I am writing a tool to strip xml files of their contents and later insert new content to the skeleton that was left.

Basically the xml looks like this beforehand:

<file>
    Some text which is not in a tag (not my decision)
    <index>Index 1</index>
    <entry att1='val1' att2='val2'>
        <text>some text</text>
        <relations>some information</relations>
    </entry>
</file> 

additional to the text and relations subelements there are other subelements. What i do is to delete all sub elements of all entry elements beside the relations element. Afterwards the file gets saved and this is where the fun starts.

I use the XmlDocument class , set the PreserveWhitespace Property to true to have all the texts keep their whitespaces and linebreaks. I save the file by using a XmlWriter created with XmlWriterSettings set to intend and to replace all linebreaks with the `Environment.NewLine.

The file ends up having all kind of entry elements in one line until a new index element starts, which has some text in it. Which is really strange, since there is text in the relations elements as well.

Using the XDoc class to try to pretty-print the output didn't work as well. I just want all elements on a new line each.

Codor
  • 17,447
  • 9
  • 29
  • 56
dm1988
  • 87
  • 2
  • 11
  • The answer I gave in [this question](http://stackoverflow.com/a/38659851/1320845) might give you some insight as to why this is happening. No easy solutions come to mind, unfortunately. – Charles Mager Jul 29 '16 at 14:57
  • Possible duplicate of [Format XML String to Print Friendly XML String](http://stackoverflow.com/questions/1123718/format-xml-string-to-print-friendly-xml-string) – Matthew Thurston Jul 29 '16 at 15:02

0 Answers0