0

I have a document on which I'm replacing the inner XML of some node:

    var xmlReplacement = File.ReadAllText(path); // this xml is well formatted with indentations itself, but not indented at the correct level for the document it's about to be inserted into

    var document = new XmlDocument();
    document.PreserveWhitespace = true;
    document.Load(path);

    // replace inner xml of ContainingNode
    var node = document.SelectSingleNode("//ContainingNode");
    node.InnerXml = xmlReplacement;

    // write back to the output file
using (var writer = new XmlTextWriter(path, null))
{
    writer.Formatting = Formatting.Indented;
    document.WriteTo(writer);
}

I end up getting the new inner xml non-indented (all the way to the left) and the close node on the same line as the close of my replacement xml's node.

How can I get this right?

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • Maybe try parsing the xmlReplacement into a new XmlDocument and using node.RemoveAll and node.AppendChild to add the replacement? – hspain Dec 12 '11 at 17:13
  • Why do you care about the indentation? XML doesn't care, so why should you? – John Saunders Dec 12 '11 at 19:16
  • Because people have to work with this file. What happens if someone runs the replacing code and checks the file into source control? Then someone else opens the xml file manually, edits something and reformats it? Now diff'ing is a pain in the ass. – Jeff Dec 12 '11 at 21:17
  • Check this answer: https://stackoverflow.com/a/1123947/3508516 – ozanmut Nov 28 '19 at 11:30

1 Answers1

0

Something similar to this may do the trick. Let the schema do the indenting work for you.

var node = document.SelectSingleNode("//ContainingNode");

node.RemoveAll();
using (var tr = XmlReader.Create(xmlReplacement))
{
    while (tr.Read())
    {
        node.AppendChild(tr);
    }
}

EDIT: Changed to remove obselete XmlTextReader EDIT 2: Changed to use a using

hspain
  • 17,528
  • 5
  • 19
  • 31
  • -1 for using `new XmlTextReader()`, deprecated since .NET 2.0. – John Saunders Dec 12 '11 at 19:15
  • Thanks. I'm not going for a strict answer, rather a direction to go in, but any input is helpful. My answer has been modified. – hspain Dec 12 '11 at 19:24
  • Same -1: how about a `using` block around `tr`? – John Saunders Dec 12 '11 at 19:27
  • Again, not looking to give the definitive way to parse XML, I'm simply pointing out a direction to pursue. Regardless, I added the using block to my example. – hspain Dec 12 '11 at 19:54
  • Our examples are copied and reused by others who don't know that we, for instance, left out the `using` block to make the example simpler. – John Saunders Dec 12 '11 at 19:55
  • 1
    XmlNode.AppendChild does not appear to take an XmlReader...that point is far more important than whether your your sample has a using block or not... – Jeff Dec 12 '11 at 21:25