2

I am trying to figure out how I can keep the XML format intact.

Ex)

I have an empty element that when I save the file to a new file it adds the element like so:

<typeOfBackup></typeOfBackup>

Instead I want it to save like:

<typeOfBackup/>

My code:

XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(@"C:\t1.xml");

XmlNode node;
node = myXmlDocument.DocumentElement;


myXmlDocument.Save(@"C:\t2.xml");
1
  • 3
    Why does it matter to you? The two are equivalent. Commented May 31, 2011 at 15:41

1 Answer 1

3

This should keep your whitespace the way you want it.

 myXmlDocument.Save(@"C:\t2.xml", SaveOptions.DisableFormatting);
Sign up to request clarification or add additional context in comments.

2 Comments

SaveOptions is not available.
SaveOptions is available on XElement - part of the system.xml.linq namespace. See here: msdn.microsoft.com/en-us/library/…. You may need to make some code changes to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.