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: 
#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(xn "assemblyIdentity").Attributes(xn "name")
                |> Seq.tryHead |> Option.map(fun v -> v.Value)
            let bd = dependentAssembly.Elements(xn "bindingRedirect")
            let oldVersion =
                bd.Attributes(xn "oldVersion")
                |> Seq.tryHead |> Option.map(fun v -> v.Value)
            let newVersion =
                bd.Attributes(xn "newVersion")
                |> Seq.tryHead |> Option.map(fun v -> v.Value)
            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 tryHead : source:seq<'T> -> 'T option
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option
val v : XAttribute
property XAttribute.Value: string with get, set
val bd : System.Collections.Generic.IEnumerable<XElement>
val oldVersion : string option
(extension) System.Collections.Generic.IEnumerable.Attributes() : System.Collections.Generic.IEnumerable<XAttribute>
(extension) System.Collections.Generic.IEnumerable.Attributes(name: XName) : System.Collections.Generic.IEnumerable<XAttribute>
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