XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("Foo"));
doc.DocumentElement.InnerXml = "Test";
StringBuilder result = new StringBuilder();
doc.WriteContentTo(XmlWriter.Create(result));
At the end, result is:
<Foo>Test
that means the end element is missing. Why is that and how can I fix it?
The problem is that you’re creating an XmlWriter, but not disposing of it – so it’s not flushing. Try this:
Output: