Adding schemaLocation attribute to XElement in LINQ to SQL
I have spent a bit of time trying to figure out how to generate an XML document with all the XML namespace paraphernalia - including schema location. I got stuck trying to create the xmlns:xsi
attribute - LINQ to XML kept creating a namespace alias for me and calling it p1
like so:
Turns out the answer is pretty simple. I was correct in my assumption that in order to create a namespace alias one must simply add an attribute to the node. However the xmlns
namespace is special and one cannot use the string xmlns
or use the default namespace. Instead one must use XNamespace.Xmlns
- like so: new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")
. Here is complete code:
The above code will generate this:
blog comments powered by Disqus
Published
28 January 2010