How to dynamically set local XML namespace?

774 views Asked by At

I have several xml namespaces that expose the same schema.

I want to use a function that dynamically accepts a namespace and applies the attributes or the properties according to the specified namespace:

Public Sub ProcessElement(element As XElement, [namespace] As XNamespace)
  element.<[namespace]:Property>.Remove()
End Sub

I actually want to set the namespace resultion withing this function to the given argument.

Look at this article, he does exactly what I want, but I am sure that in VB.NET XML Literals there is a much nicer way than doing it the hardcoded way.

I know I can simply do it like this:

element.Elements([namespace] + "Property").Remove()

But I was wondering if there is a way to include namespaces in VB.NET amazing .<>, .@, ...<> language shortcuts.

1

There are 1 answers

0
Shimmy Weitzhandler On BEST ANSWER

I actually found a better solution having the dynamic namespace being retrieved from the element itself:

Public Sub ProcessElement(element As XElement)  
  Dim [namespace] = element.GetDefaultNamespace()
  element.<[namespace]:Property>.Remove()  
End Sub 

There are also a few other namespace-manipulating methods in the XElement, so it's easier to make it real dynamic, without even need to know what the namespaces are.