LINQ to XML : Working with Prefixes

LINQ to XML : Working with Prefixes

  • Comments 5

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 />

</pfx:root>

 

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

 

XElement root = new XElement(ns+"root",

    new XAttribute(XNamespace.Xmlns + "pfx", ns),

    new XElement(ns+"child"));

 

Namoskar!!!

Leave a Comment
  • Please add 7 and 8 and type the answer here:
  • Post
  • Thats probably my biggest complaint with The XLINQ API. There's no way to specify a default namespace, so you always end up having to prepend the namespace on every thing you do. Terribly annoying.

    VB "solved" this by adding some language specific directives (gross). This should just be something easily configurable in the API.

  • If you have a lot of children, you will see a lot of code like (which is very awkward)

    new XElement(ns+"child")

    which hints we might need a new way to create XElement that takes a namespace or we could set up a default namespace somewhere

  • @Anonymous,

    That's true I am trying to find it out...

  • Welcome to the forty-third issue of Community Convergence. The last few weeks have been consumed by the

  • I wanted to know how to query an element with prefix

Page 1 of 1 (5 items)