4 people like it.

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

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
#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
namespace System
namespace System.Xml
namespace System.Xml.Linq
val parseBindingRedirects : filename:string -> (string * string * string) list
val filename : string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val xn : (string -> XName)
val s : string
type XName =
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member LocalName : string
  member Namespace : XNamespace
  member NamespaceName : string
  member ToString : unit -> string
  static member Get : expandedName:string -> XName + 1 overload
XName.Get(expandedName: string) : XName
XName.Get(localName: string, namespaceName: string) : XName
val xml : XDocument
Multiple items
type XDocument =
  inherit XContainer
  new : unit -> XDocument + 3 overloads
  member Declaration : XDeclaration with get, set
  member DocumentType : XDocumentType
  member NodeType : XmlNodeType
  member Root : XElement
  member Save : stream:Stream -> unit + 6 overloads
  member SaveAsync : writer:XmlWriter * cancellationToken:CancellationToken -> Task + 2 overloads
  member WriteTo : writer:XmlWriter -> unit
  member WriteToAsync : writer:XmlWriter * cancellationToken:CancellationToken -> Task
  static member Load : uri:string -> XDocument + 7 overloads
  ...

--------------------
XDocument() : XDocument
XDocument([<System.ParamArray>] content: obj []) : XDocument
XDocument(other: XDocument) : XDocument
XDocument(declaration: XDeclaration, [<System.ParamArray>] content: obj []) : XDocument
XDocument.Load(reader: System.Xml.XmlReader) : XDocument
XDocument.Load(textReader: System.IO.TextReader) : XDocument
XDocument.Load(stream: System.IO.Stream) : XDocument
XDocument.Load(uri: string) : XDocument
XDocument.Load(reader: System.Xml.XmlReader, options: LoadOptions) : XDocument
XDocument.Load(textReader: System.IO.TextReader, options: LoadOptions) : XDocument
XDocument.Load(stream: System.IO.Stream, options: LoadOptions) : XDocument
XDocument.Load(uri: string, options: LoadOptions) : XDocument
val depAssemblies : System.Collections.Generic.IEnumerable<XElement>
XContainer.Descendants() : System.Collections.Generic.IEnumerable<XElement>
XContainer.Descendants(name: XName) : System.Collections.Generic.IEnumerable<XElement>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
val dependentAssembly : XElement
val name : string option
XContainer.Elements() : System.Collections.Generic.IEnumerable<XElement>
XContainer.Elements(name: XName) : System.Collections.Generic.IEnumerable<XElement>
module Seq

from Microsoft.FSharp.Collections
val tryFind : predicate:('T -> bool) -> source:seq<'T> -> 'T option
val e : XElement
property XElement.Name: XName with get, set
property XName.LocalName: string with get
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option
XElement.Attributes() : System.Collections.Generic.IEnumerable<XAttribute>
XElement.Attributes(name: XName) : System.Collections.Generic.IEnumerable<XAttribute>
val a : XAttribute
property XAttribute.Name: XName with get
val v : XAttribute
property XAttribute.Value: string with get, set
val flatten : option:'T option option -> 'T option
val bd : XElement option
val oldVersion : string option
val b : XElement
val newVersion : string option
union case Option.Some: Value: 'T -> Option<'T>
val n : string
val ov : string
val nv : string
val distinct : source:seq<'T> -> seq<'T> (requires equality)
val toList : source:seq<'T> -> 'T list

More information

Link:http://fssnip.net/88S
Posted:1 year ago
Author:Tuomas Hietanen
Tags: configuration , linq-to-xml , parsing