Marvin
Marvin

Reputation: 41

Editing xml files and preserving whitespaces and tabs

I have an XML document that would contain empty nodes that looked like the following:

<metadata territory="USA"></metadata>

After simply opening, then saving using XmlDocument, this line looks like:

  <metadata territory="USA">  
  </metadata>

When I set PreserveWhitespace to true, it converted the entire XML to 1 line, so this won't work.

These XML files need to keep the current formatting as much as possible. I know, technically, it doesn't matter which way they are written, they will be read the same way but I still need to keep the same formatting. I can't figure out a way to keep the nodes with no values to 1 line. Is there a way to do this?

The ONLY method that keeps the document in its original formatting is if the XML file contained 'xml:space="preserve"' in the header, but I am to leave the header as is.

The only thing I want to change is the addition of values. As I said, simply loading and saving a document adds this, so if you want to test, just try...

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Temp\test.xml");
doc.Save(@"C:\Temp\test_02.xml");

Upvotes: 4

Views: 5623

Answers (5)

WickedFan
WickedFan

Reputation: 356

If you're using xmlDocument, I may recommend you to use XDocument instead (Framework 3.0+).

PreserveWhitespace will add a

<whatever> <...>
**</whatever>**

to each line while None will just close it like <... />.

I looked up for 5 minutes how to preserve those white space, but couldn't find it. There's something ommitting char(13) in the de/reserialization.

XDocument doc;
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
   //Alternative with .None
   doc = XDocument.Load(fs, LoadOptions.PreserveWhitespace);
}

and importantly..

 xmlDoc.Save("lala.xml", SaveOptions.None);

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

I don't think this is possible. When you load the XML document you lose formatting information; so there is no way that Save can give the same results.

Upvotes: 0

Larry Osterman
Larry Osterman

Reputation: 16142

I'm with Richard Schneider: I don't believe it's possible. One possible solution is to take the output XML file and run it through an XML formatting program that normalizes the format of the XML file (you can probably write one with the unmanaged XML dom if one can't be found).

Since the file is always normalized, it won't change that much hopefully.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160882

Just did the test and this works using both XDocument and XmlDocument by setting the PreserveWhitespace property.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("test.xml");
xmlDoc.Save("testOut.xml");
..
XDocument xdoc = XDocument.Load("test.xml", LoadOptions.PreserveWhitespace);
xdoc.Save(@"testOut.xml");

Input:

<foo>
  <metadata territory="USA"></metadata>
  <bar></bar>
  <baz>
  </baz>
</foo>

Output:

<foo>
  <metadata territory="USA"></metadata>
  <bar></bar>
  <baz>
  </baz>
</foo>

Upvotes: 8

Andy  Gee
Andy Gee

Reputation: 3335

Why not save the file as a different format, then rename it back to XML after it's been saved. I would be surprised if it still gets formatted incorrectly. Not pretty but pretty easy.

Upvotes: -1

Related Questions