Hi,
I am trying to replace a node using ReplaceWith(), but noticed that it results in badly formated XML (missing new lines and indentations).
Has anyone has come across this problem before?
Code Snippet:
[Test]
public void Test()
{
XDocument document;
using (var reader = XmlReader.Create("C:\\test.xml"))
{
// *** Running this line results in new lines OMITTED ***
document = XDocument.Load(reader);
// *** Running this line results in proper formatting ***
//document = XDocument.Parse(XDocument.Load(reader).ToString());
}
var newNode = new XElement("Node", new XElement("SubNode"));
document.Root.Element("Node").ReplaceWith(newNode);
Console.Out.WriteLine("document = {0}", document);
}
Steps to Reproduce:
1) Create C:\test.xml with the following:
<Test>
<Node/>
<Test>
2) Run the code snippet above.
This will result in some in some improperly formated XML:
<Test>
<Node><SubNode /></Node>
</Test>
3) Uncomment this line:
document = XDocument.Parse(XDocument.Load(reader).ToString());
4) Run the snippet again.
The result will be properly formatted:
<Test>
<Node>
<SubNode />
</Node>
</Test>