开发者

How to dynamically set local XML namespace?

开发者 https://www.devze.com 2023-01-13 18:38 出处:网络
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 namesp

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.

开发者_StackOverflow社区

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号