Search code examples
.netxmldocument

.NET XmlDocument preserve whitespace


I am parsing a XML document with XmlDocument. When the source file contains

<template>
  <name>ABC</name>
  <version>1</version>
  ABC
  DEF
  <author />
  ABC

I often need to get the pure underlying XML and I use InnerXml for this. But InnerXml discards all whitespace between two XML elements when there is only whitespace. Meaning the fragement above is as InnerXml not what you see above - instead it is:

<template><name>ABC</name><version>1</version>
  ABC
  DEF
  <author />
  ABC

How can I get the content of a XmlNode without the discarded whitespace? It must be somewhere in the XmlDocument because when saving it with Save it will output like loaded (meaning like the first example).


Solution

  • Unless I'm missing something, you can just ask the XMlDocument to PreserveWhitespace and you should get "WYSIWYG" loading/saving.