tags:

views:

674

answers:

1

This works fine:

     XDocument xdoc = new XDocument(
        new XDeclaration("1.1", "UTF-8", "yes"),
        new XProcessingInstruction("foo", "bar"),
        new XElement("test"));

But if I change it to pass the "params array" explicitly as an array, it fails with this System.ArgumentException: Non white space characters cannot be added to content.:

     object[] content = new object[] {
        new XDeclaration("1.1", "UTF-8", "yes"),
        new XProcessingInstruction("foo", "bar"),
        new XElement("test")
     };
     xdoc = new XDocument(content);

Aren't these two examples exactly equivalent? What's going on here?

+3  A: 

When you use the first method, you're using the overload of XDocument that first takes an XDeclaration and then a params for the content. However, when you're using the second approach, you're using the overload which takes a params for content. The XDeclaration in your object[] array is coming through as content, and that's where it's blowing up.

See here : http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.xdocument.aspx

BFree
I see; I didn't realize I was calling different overloads. It's seems a bit unfortunate that the `XDocument(object[])` accepts all sorts of objects but for some reason excludes XDeclaration.
Wim Coenen