Wriju's BLOG

.NET, Cloud and everything

LINQ to XML : Working with Prefixes

Based on my previous post on LINQ to XML : Working with Namespaces, if you want to add prefixes to your Xml things becomes little crazy. The trick is that you have to use XAttribute() to attach prefixes. So the Xml as below need the following code,  


<pfx:root xmlns:pfx=urn:mynamespace-com>

  <pfx:child />



XNamespace ns = XNamespace.Get(“urn:mynamespace-com”);


XElement root = new XElement(ns+“root”,

    new XAttribute(XNamespace.Xmlns + “pfx”, ns),

    new XElement(ns+“child”));