I am trying to add an XDeclaration to an XML document like so:
XDocument doc = new XDocument();
XDeclaration dc = new XDeclaration("1.0", "utf-8", "no");
XNamespace ns = "http://www.foo.com/bar";
doc.Add(dc);
However, I get the following error:
Non white space characters cannot be added to content.
If I take away the XDeclaration line the code works fine, where am I going wrong?
You want to set the declaration like this, not with
.Addlike you tried:Or it can be set when instantiating the xDocument with this constructor