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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s