Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new XElements with new line in XDocument with PreserveWhitespace LoadOptions

I'm trying to edit XML file saving its format:

<root>
    <files>
        <file>a</file>

        <file>b</file>
        <file>c</file>



        <file>d</file>
    </files>
</root>

So i load xml document using
XDocument xDoc = XDocument.Load(path, LoadOptions.PreserveWhitespace);
But when i'm trying to add new elements
xDoc.Root.Element("files").Add(new XElement("test","test")); xDoc.Root.Element("files").Add(new XElement("test2","test2"));
it adds in the same line, so output is like:

<root>
    <files>
        <file>a</file>

        <file>b</file>
        <file>c</file>



        <file>d</file>
    <test>test</test><test2>test2</test2></files>
</root>

So how can i add new elements each on new line saving initial formatting? I tried to use XmlWriter with Setting.Indent = true to save XDocument, but as i see, elements are added to the same line, when i use xDoc.Root.Element().Add()

Update: full part of program loading, modifying and saving document

using System;
using System.Xml;
using System.Xml.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {            
            string path = @".\doc.xml";    
            XDocument xDoc = XDocument.Load(path, LoadOptions.PreserveWhitespace);

            //when i debug i see in "watch" that after these commands new elements are already added in same line
            xDoc.Descendants("files").First().Add(new XElement("test", "test"));
            xDoc.Descendants("files").First().Add(new XElement("test2", "test2"));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            settings.IndentChars = "\t";

            using (XmlWriter writer = XmlTextWriter.Create(path, settings))
            {                
                xDoc.Save(writer);
                //Here i also tried save without writer - xDoc.Save(path)
            }
        }
    }
}
like image 549
Alexeev Mikhail Avatar asked Dec 18 '14 13:12

Alexeev Mikhail


2 Answers

The problem appears to be caused by your use of LoadOptions.PreserveWhitespace. This seems to trump XmlWriterSettings.Indent - you've basically said, "I care about this whitespace"... "Oh, now I don't."

If you remove that option, just using:

XDocument xDoc = XDocument.Load(path);

... then it indents appropriately. If you want to preserve all the original whitespace but then indent just the new elements, I think you'll need to add that indentation yourself.

like image 135
Jon Skeet Avatar answered Oct 21 '22 11:10

Jon Skeet


I had a similar problem and I could solve with the code below:

var newPolygon = new XElement(doc.Root.GetDefaultNamespace() + "polygon");
groupElement.Add(newPolygon); 
groupElement.Add(Environment.NewLine); 

I hope this code can help some people...

like image 43
Robson Avatar answered Oct 21 '22 09:10

Robson