0

How can one insert the 'XML Base' attribute/namespace (http://en.wikipedia.org/wiki/XML_Base) into the root of an XElement or XDocument?

<feed xml:base="someurl.com">...</feed>

And, do that without messing up the whole XElement (e.g. by it trying to insert the same everywhere else or some related crazy side effect).

Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69
  • 1
    (From top of my head) Have you tried `element.Root.SetAttribute(XNamespace.Xml + "base", "someurl.com")` ? – Pawel Dec 06 '12 at 01:17
  • Sir... you rock! Yes, that works. (With the change for XElement x - x.SetAttributeValue(XNamespace.Xml + "base", "someurl.com");) (note: 'Value'). Please post as an answer. But while you are at it, ... is there a way to do the same for a simply xmlns (not named one), I just need a root default xmlns like so: . The important thing there is I do not want it to populate that namespace in any descendant elements ... this is why I hate namespaces in XElement. Thanks much. – Nicholas Petersen Dec 06 '12 at 01:23
  • The question I have would be what "do not wnat it to populate the namespace to descendant elements" means. Do you want the descendant elements to be in this namespace but do not have their own xmlns attributes or you want only the given element to be in the namespace but descendant elements should be in empty namespace? In any case there is not an easy way to do it. As you probably noticed XName properties (inluding NamespaceName) are readonly. Changing Namespace is efectively changing the name of the element (and possibly all descendants) and therefore impossible without recreating the elemen – Pawel Dec 06 '12 at 04:45
  • Thanks Pawel. I think unfortunately, due to the one great shortcoming of Linq to Xml, is that it does not allow a default namespace without making someone go through the ridiculous hoopla of having to add it everywhere. Now when you have to add more than a default namespace, then thing work great fro them. Problem is, in a huge number of documents, you have to specify 1 namespace at the top. Suddenly that means you can't simply construct the thing like before. But true XML DOES allow a default namespace (descendants inherit)! So in this regard, they were not being true to XML. – Nicholas Petersen Dec 06 '12 at 17:03
  • There are two ways of setting namespaces - you can use xmlns in which case all the descenendant elenents (but not attributes) will derive it and will belong to this namespaces until it is redefined (if you want to reset to the empty namespace you just add xmlns="" to the element) or - if you wan just one specific element belong to a certain non-empty namespace - you can bing a namespace Uri to the namespace prefix (by using xsmlns:ns1="{my-namespace-uri}" either on the element itself or the document element) and use ns1:{elementName} notation to put a single element to this namespace. – Pawel Dec 06 '12 at 17:17

1 Answers1

2

This should do the trick:

element.Root.SetAttributeValue(XNamespace.Xml + "base", "someurl.com") 

(Edit: SetAttributeValue)

Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69
Pawel
  • 31,342
  • 4
  • 73
  • 104