All, I want to create a soap envelope xml document eg.
You need to combine the XName of your XAttribute with an XNamespace. I know right... Anyhow try this.
XNamespace soap = "http://www.w3.org/2001/12/soap-envelope";
XAttribute encoding = new XAttribute(soap + "encodingStyle",
"http://www.w3.org/2001/12/soap-encoding");
Specify the namespace when you create the 'encodingStyle' XAttribute (by using ns + "encodingStyle"):
XAttribute encoding = new XAttribute(ns + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding");
The two-parameter XAttribute constructor takes an XName as the first argument. This can either be constructed implicitly from a string (as in the code in your question), or directly by "adding" a string to an XNamespace to create an XName (as above).
You need to use the XName.Get method to construct the attribute name with a namespace:
var xName = XName.Get("myAttributeName", "http://www.w3.org/2001/XMLSchema-instance");
var attr = new XAttribute(xName, "myAttributeValue");