I am trying to edit an XML file while maintaining its format:
<root> <files> <file>a</file> <file>b</file> <file>c</file> <file>d</file> </files> </root>
So, I am loading an xml document using XDocument xDoc = XDocument.Load(path, LoadOptions.PreserveWhitespace);
But when I try to add new elements
xDoc.Root.Element("files").Add(new XElement("test","test")); xDoc.Root.Element("files").Add(new XElement("test2","test2"));
it adds to one line, so the output is as follows:
<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 to a new line while retaining the original formatting? I tried using XmlWriter with Setting.Indent = true to save the XDocument, but as I see it, the elements are added to the same line when I use xDoc.Root.Element().Add()
Update: the full part of downloading, modifying and saving documents
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);
source share