Cannot write XML declaration. WriteStartDocument method has already written it.

Possibly save 1 hour of your time: I was calling an api service that returns xml. I have to pass in a xmlNode element. For some reason, I get the error:

Cannot write XML declaration. WriteStartDocument method has already written it.

Solution: pass in DocumentElement instead of FirstChild

var doc = new XmlDocument();

doc.LoadXml(queryMsg);
var xmlNode = doc.DocumentElement; // instead of doc.FirstChild

ExportTool.Web.org.un.data.NSIStdV20Service service = new ExportTool.Web.org.un.data.NSIStdV20Service();

var xmlData = service.GetCompactData(xmlNode);

Book XML in a Nutshell, Third Edition

Advertisements