LINQ to XML in VB.NET and Using the Right Language for the Job
I'm almost always using C# in my .NET projects, unless I'm doing Office automation where the VB-way of dealing with optional parameters helps out making the code a bit cleaner.
The last week we've been upgrading ASMX-clients to become WCF-clients for a number of old .NET 1.1 and 2.0 projects, and we ended up with a bunch of app.config files with loads and loads of WCF client endpoint sections, each of them pointing at their own binding configuration. To manually clean this up would take hours and hours of tedious work which would probably result in more than a few errors.
So I thought maybe I could do search/replace with a regexp-capable editor... or try out XML Literals in VB.NET. I wanted to remove old behaviors, extensions and bindings, then add my own behaviors and extensions and finally change some attributes on each client endpoint. Doing this with XML Literals and XDocument/XElement in VB.NET was quite straight forward and didn't result in too many lines of code:
Sub FixupConfig(ByVal infile As String, ByVal outfile As String)
Dim defaultBindingConfigurationXml = <binding name="defaultBindingConfiguration"
Dim behaviorsXml = <behaviors>
Dim extensionsXml = <extensions>
type="SomeType.SetMaxFaultSizeBehavior, SomeAssembly, Version=188.8.131.52,
type="SomeType.ClientExceptionHandler, SomeAssembly, Version=184.108.40.206,
Dim xdoc = XDocument.Load(infile)
Dim elems = xdoc...<client>.<endpoint>
For Each element In elems
element.@bindingConfiguration = "defaultBindingConfiguration"
element.@behaviorConfiguration = "FacadeSoapEndpointBehavior"
I've heard people recommend to use VB.NET when dealing with XML and I agree - use the right language for the job. When doing this - remember to import System.Linq or you'll miss some vital extensions like the .Add() and .Single() methods ;)