6 people like it.

Write graph as DGML file

Write graph as dgml file

 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: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
// see example at the bottom
module dgml

open System
open System.Collections.Generic
open System.Xml
open System.Xml.Serialization

type Graph() = 
    [<DefaultValue>] val mutable public Nodes : Node[]
    [<DefaultValue>] val mutable public Links : Link[]    

and Node() =
    [<XmlAttribute>] member val Id = "" with get, set
    [<XmlAttribute>] member val Label = "" with get, set

and Link()  =
    [<XmlAttribute>] member val Source = "" with get, set
    [<XmlAttribute>] member val Target = "" with get, set
    [<XmlAttribute>] member val Label  = "" with get, set

type DGMLWriter() =
    let Nodes = new List<Node>()
    let Links = new List<Link>()
    member m.AddNode id label = Nodes.Add(new Node(Id=id, Label=label))
    member m.AddLink src  trg  label = Links.Add(new Link(Source=src, Target=trg, Label=label))
    member m.Write (filename : string) =
        let g = Graph(Nodes=Nodes.ToArray(), Links=Links.ToArray())
        let root = new XmlRootAttribute("DirectedGraph")
        root.Namespace <- "http://schemas.microsoft.com/vs/2009/dgml"
        let serializer = new XmlSerializer(typeof<Graph>, root)
        let settings = new XmlWriterSettings(Indent=true)
        use xmlWriter = XmlWriter.Create(filename, settings)
        serializer.Serialize(xmlWriter, g);

// F# translation of http://stackoverflow.com/questions/8199600/c-sharp-directed-graph-generating-library

// create a graph and write it as dgml. Open the graph with Visual Studio for Visualization. 
let w = DGMLWriter()
w.AddNode "bugs" "bugs bunny"
w.AddNode "babsi" "babsi bunny"
w.AddNode "toons" "loony toons"
w.AddLink "toons" "bugs" "contains"
w.AddLink "toons" "babsi" "contains"
w.Write "m:/a.dgml"
module dgml
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Xml
namespace System.Xml.Serialization
Multiple items
type Graph =
  new : unit -> Graph
  val mutable Nodes: Node []
  val mutable Links: Link []

Full name: dgml.Graph

--------------------
new : unit -> Graph
Multiple items
type DefaultValueAttribute =
  inherit Attribute
  new : unit -> DefaultValueAttribute
  new : check:bool -> DefaultValueAttribute
  member Check : bool

Full name: Microsoft.FSharp.Core.DefaultValueAttribute

--------------------
new : unit -> DefaultValueAttribute
new : check:bool -> DefaultValueAttribute
Graph.Nodes: Node []
Multiple items
type Node =
  new : unit -> Node
  member Id : string
  member Label : string
  member Id : string with set
  member Label : string with set

Full name: dgml.Node

--------------------
new : unit -> Node
Graph.Links: Link []
Multiple items
type Link =
  new : unit -> Link
  member Label : string
  member Source : string
  member Target : string
  member Label : string with set
  member Source : string with set
  member Target : string with set

Full name: dgml.Link

--------------------
new : unit -> Link
Multiple items
type XmlAttribute =
  inherit XmlNode
  member AppendChild : newChild:XmlNode -> XmlNode
  member BaseURI : string
  member CloneNode : deep:bool -> XmlNode
  member InnerText : -> string with set
  member InnerXml : -> string with set
  member InsertAfter : newChild:XmlNode * refChild:XmlNode -> XmlNode
  member InsertBefore : newChild:XmlNode * refChild:XmlNode -> XmlNode
  member LocalName : string
  member Name : string
  member NamespaceURI : string
  ...

Full name: System.Xml.XmlAttribute

--------------------
type XmlAttributeAttribute =
  inherit Attribute
  new : unit -> XmlAttributeAttribute + 3 overloads
  member AttributeName : string with get, set
  member DataType : string with get, set
  member Form : XmlSchemaForm with get, set
  member Namespace : string with get, set
  member Type : Type with get, set

Full name: System.Xml.Serialization.XmlAttributeAttribute

--------------------
XmlAttributeAttribute() : unit
XmlAttributeAttribute(attributeName: string) : unit
XmlAttributeAttribute(type: Type) : unit
XmlAttributeAttribute(attributeName: string, type: Type) : unit
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set
Multiple items
type DGMLWriter =
  new : unit -> DGMLWriter
  member AddLink : src:string -> trg:string -> label:string -> unit
  member AddNode : id:string -> label:string -> unit
  member Write : filename:string -> unit

Full name: dgml.DGMLWriter

--------------------
new : unit -> DGMLWriter
val Nodes : List<Node>
Multiple items
type List<'T> =
  new : unit -> List<'T> + 2 overloads
  member Add : item:'T -> unit
  member AddRange : collection:IEnumerable<'T> -> unit
  member AsReadOnly : unit -> ReadOnlyCollection<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Capacity : int with get, set
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  ...
  nested type Enumerator

Full name: System.Collections.Generic.List<_>

--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
val Links : List<Link>
val m : DGMLWriter
member DGMLWriter.AddNode : id:string -> label:string -> unit

Full name: dgml.DGMLWriter.AddNode
val id : string
val label : string
List.Add(item: Node) : unit
member DGMLWriter.AddLink : src:string -> trg:string -> label:string -> unit

Full name: dgml.DGMLWriter.AddLink
val src : string
val trg : string
List.Add(item: Link) : unit
member DGMLWriter.Write : filename:string -> unit

Full name: dgml.DGMLWriter.Write
val filename : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val g : Graph
List.ToArray() : Node []
List.ToArray() : Link []
val root : XmlRootAttribute
Multiple items
type XmlRootAttribute =
  inherit Attribute
  new : unit -> XmlRootAttribute + 1 overload
  member DataType : string with get, set
  member ElementName : string with get, set
  member IsNullable : bool with get, set
  member Namespace : string with get, set

Full name: System.Xml.Serialization.XmlRootAttribute

--------------------
XmlRootAttribute() : unit
XmlRootAttribute(elementName: string) : unit
property XmlRootAttribute.Namespace: string
val serializer : XmlSerializer
Multiple items
type XmlSerializer =
  new : xmlTypeMapping:XmlTypeMapping -> XmlSerializer + 8 overloads
  member CanDeserialize : xmlReader:XmlReader -> bool
  member Deserialize : stream:Stream -> obj + 5 overloads
  member Serialize : textWriter:TextWriter * o:obj -> unit + 7 overloads
  event UnknownNode : XmlNodeEventHandler
  event UnknownAttribute : XmlAttributeEventHandler
  event UnknownElement : XmlElementEventHandler
  event UnreferencedObject : UnreferencedObjectEventHandler
  static member FromMappings : mappings:XmlMapping[] -> XmlSerializer[] + 2 overloads
  static member FromTypes : types:Type[] -> XmlSerializer[]
  ...

Full name: System.Xml.Serialization.XmlSerializer

--------------------
XmlSerializer(xmlTypeMapping: XmlTypeMapping) : unit
XmlSerializer(type: Type) : unit
XmlSerializer(type: Type, root: XmlRootAttribute) : unit
XmlSerializer(type: Type, extraTypes: Type []) : unit
XmlSerializer(type: Type, overrides: XmlAttributeOverrides) : unit
XmlSerializer(type: Type, defaultNamespace: string) : unit
XmlSerializer(type: Type, overrides: XmlAttributeOverrides, extraTypes: Type [], root: XmlRootAttribute, defaultNamespace: string) : unit
XmlSerializer(type: Type, overrides: XmlAttributeOverrides, extraTypes: Type [], root: XmlRootAttribute, defaultNamespace: string, location: string) : unit
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val settings : XmlWriterSettings
Multiple items
type XmlWriterSettings =
  new : unit -> XmlWriterSettings
  member CheckCharacters : bool with get, set
  member Clone : unit -> XmlWriterSettings
  member CloseOutput : bool with get, set
  member ConformanceLevel : ConformanceLevel with get, set
  member Encoding : Encoding with get, set
  member Indent : bool with get, set
  member IndentChars : string with get, set
  member NamespaceHandling : NamespaceHandling with get, set
  member NewLineChars : string with get, set
  ...

Full name: System.Xml.XmlWriterSettings

--------------------
XmlWriterSettings() : unit
val xmlWriter : XmlWriter
type XmlWriter =
  member Close : unit -> unit
  member Flush : unit -> unit
  member LookupPrefix : ns:string -> string
  member Settings : XmlWriterSettings
  member WriteAttributeString : localName:string * value:string -> unit + 2 overloads
  member WriteAttributes : reader:XmlReader * defattr:bool -> unit
  member WriteBase64 : buffer:byte[] * index:int * count:int -> unit
  member WriteBinHex : buffer:byte[] * index:int * count:int -> unit
  member WriteCData : text:string -> unit
  member WriteCharEntity : ch:char -> unit
  ...

Full name: System.Xml.XmlWriter
XmlWriter.Create(output: XmlWriter) : XmlWriter
XmlWriter.Create(output: Text.StringBuilder) : XmlWriter
XmlWriter.Create(output: IO.TextWriter) : XmlWriter
XmlWriter.Create(output: IO.Stream) : XmlWriter
XmlWriter.Create(outputFileName: string) : XmlWriter
XmlWriter.Create(output: XmlWriter, settings: XmlWriterSettings) : XmlWriter
XmlWriter.Create(output: Text.StringBuilder, settings: XmlWriterSettings) : XmlWriter
XmlWriter.Create(output: IO.TextWriter, settings: XmlWriterSettings) : XmlWriter
XmlWriter.Create(output: IO.Stream, settings: XmlWriterSettings) : XmlWriter
XmlWriter.Create(outputFileName: string, settings: XmlWriterSettings) : XmlWriter
XmlSerializer.Serialize(xmlWriter: XmlWriter, o: obj) : unit
XmlSerializer.Serialize(stream: IO.Stream, o: obj) : unit
XmlSerializer.Serialize(textWriter: IO.TextWriter, o: obj) : unit
XmlSerializer.Serialize(xmlWriter: XmlWriter, o: obj, namespaces: XmlSerializerNamespaces) : unit
XmlSerializer.Serialize(stream: IO.Stream, o: obj, namespaces: XmlSerializerNamespaces) : unit
XmlSerializer.Serialize(textWriter: IO.TextWriter, o: obj, namespaces: XmlSerializerNamespaces) : unit
XmlSerializer.Serialize(xmlWriter: XmlWriter, o: obj, namespaces: XmlSerializerNamespaces, encodingStyle: string) : unit
XmlSerializer.Serialize(xmlWriter: XmlWriter, o: obj, namespaces: XmlSerializerNamespaces, encodingStyle: string, id: string) : unit
val w : DGMLWriter

Full name: dgml.w
member DGMLWriter.AddNode : id:string -> label:string -> unit
member DGMLWriter.AddLink : src:string -> trg:string -> label:string -> unit
member DGMLWriter.Write : filename:string -> unit

More information

Link:http://fssnip.net/lq
Posted:7 years ago
Author:loony
Tags: dgml graph