Skip to content

Instantly share code, notes, and snippets.

@Thorium
Last active February 14, 2023 13:46
Show Gist options
  • Save Thorium/1deadba6f901519c7661286df2d758cf to your computer and use it in GitHub Desktop.
Save Thorium/1deadba6f901519c7661286df2d758cf to your computer and use it in GitHub Desktop.
Parse bindingRedirects with Linq2Xml
// Find binding-redirects with linq-to-xml from a .config file.
// This might be useful for then parsing config-changes e.g. by System.Linq.Enumerable.Except
#r @"System.Xml.Linq.dll"
open System.Xml.Linq
let parseBindingRedirects (filename:string) =
let xn s = XName.Get(s,"urn:schemas-microsoft-com:asm.v1")
let xml = XDocument.Load filename
let depAssemblies = xml.Descendants(xn "dependentAssembly")
seq {
for dependentAssembly in depAssemblies do
let name =
dependentAssembly.Elements()
|> Seq.tryFind(fun e -> e.Name.LocalName = "assemblyIdentity")
|> Option.map(fun e ->
e.Attributes()
|> Seq.tryFind(fun a -> a.Name.LocalName = "name")
|> Option.map(fun v -> v.Value)
) |> Option.flatten
let bd =
dependentAssembly.Elements()
|> Seq.tryFind(fun e -> e.Name.LocalName = "bindingRedirect")
let oldVersion =
bd |> Option.map(fun b ->
b.Attributes()
|> Seq.tryFind(fun a -> a.Name.LocalName = "oldVersion")
|> Option.map(fun v -> v.Value)) |> Option.flatten
let newVersion =
bd |> Option.map(fun b ->
b.Attributes()
|> Seq.tryFind(fun a -> a.Name.LocalName = "newVersion")
|> Option.map(fun v -> v.Value)) |> Option.flatten
match name, oldVersion, newVersion with
| Some n, Some ov, Some nv -> yield n, ov, nv
| _ -> ()
} |> Seq.distinct |> Seq.toList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment