Adding schemaLocation attribute to XElement in LINQ to XML

3. June 2010 21:26 by viperguynaz in Windows  //  Tags: ,   //   Comments (0)

The MSDN library has a good section on working with XML Namespaces in LINQ to XML. XML names are definitely a source of complexity in XML programming. An XML name consists of an XML namespace (also called an XML namespace URI) and a local name. An XML namespace is similar to a namespace in a .NET Framework-based program. It enables you to uniquely qualify the names of elements and attributes. This helps avoid name conflicts between various parts of an XML document. When you have declared an XML namespace, you can select a local name that only has to be unique within that namespace.

 

In LINQ to XML, the class that represents XML names is XName. XML names appear frequently throughout the LINQ to XML API, and wherever an XML name is required, you will find an XName parameter. However, you rarely work directly with an XName.  Typically in LINQ to XML you use XNamespace.

 

The problem I ran in to for a project was modifying the default namespace and including a schemaLocation.  LINQ to XML kept creating a namespace alias for me and calling it p1 like so:

<rootnode p1:xsi="http://www.w3.org/2001/XMLSchema-instance" p1="http://www.foo.bar" xmlns="http://www.foo.bar">
</rootnode>

Thanks to Igor Zevka at http://www.somethingorothersoft.com I was able to find a solution - http://www.somethingorothersoft.com/2010/01/28/adding-schemalocation-attribute-to-xelement-in-linq-to-sql/In order to create a namespace alias, you simply add an attribute to the node. However the xmlns namespace is special and you cannot use the string xmlns or use the default namespace. Instead you must use XNamespace.Xmlns – like so:

new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")

To generate the following root node and namespaces:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.foo.bar someSchema.xsd" xmlns="http://www.foo.bar" >
</root>

Use the following code:

XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace defaultNamespace = XNamespace.Get("http://www.foo.bar");
 
XElement doc = new XElement(
   new XElement(defaultNamespace + "root",
       new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
       new XAttribute(xsi + "schemaLocation", "http://www.foo.bar someSchema.xsd")
   )
);

Be aware - if you want to add elements to the document, you need to specify the defaultNamespace in the element name or you will get xmlns="" added to your element. For example, to add a child element "count" to the above document, use:

xdoc.Add(new XElement(defaultNamespace + "count", 0)

Cheers!

TBTJE9ZQEW47

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

After 20 years flying F-16s in the USAF, I've had a ton of fun and I'm now doing something I enjoy almost as much, developing code.  I currently work as an ASP.NET/MVC developer for SpinSix Strategic Marketing Design in Scottsdale AZ.  I love developing ASP.NET MVC and eCommerce sites.

Month List

Page List