FSharp.Data.Xsd


Tutorial

Once the needed libraries have been referenced and the namespace is open:

1: 
2: 
3: 
4: 
#r "System.Xml.Linq.dll"
#r "FSharp.Data.Xsd.dll"

open FSharp.Data

an XML schema can be specified either as plain text:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
type Foo = XmlProvider<Schema = """
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="foo">
      <xs:complexType>
        <xs:attribute name="bar" type="xs:string" use="required" />
        <xs:attribute name="baz" type="xs:int" />
      </xs:complexType>
    </xs:element>
  </xs:schema>""">

or as a file:

1: 
type Foo' = XmlProvider<Schema="c:/temp/foo.xsd">

When the file includes other schema files, the ResolutionFolder parameter can help locating them:

1: 
type Foo'' = XmlProvider<Schema="foo.xsd", ResolutionFolder="c:/temp">

A schema may define multiple root elements:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
type TwoRoots = XmlProvider<Schema = """
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root1">
      <xs:complexType>
        <xs:attribute name="foo" type="xs:string" use="required" />
        <xs:attribute name="fow" type="xs:int" />
      </xs:complexType>
    </xs:element>
    <xs:element name="root2">
      <xs:complexType>
        <xs:attribute name="bar" type="xs:string" use="required" />
        <xs:attribute name="baz" type="xs:date" use="required" />
      </xs:complexType>
    </xs:element>
  </xs:schema>    
""">

In this case the provided type has an optional property for each alternative:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
let e1 = TwoRoots.Parse "<root1 foo='aa' fow='2' />"
match e1.Root1, e1.Root2 with
| Some x, None -> 
    assert(x.Foo = "aa")
    assert(x.Fow = Some 2)
| _ -> failwith "Unexpected"
let e2 = TwoRoots.Parse "<root2 bar='aa' baz='2017-12-22' />"
match e2.Root1, e2.Root2 with
| None, Some x -> 
    assert(x.Bar = "aa")
    assert(x.Baz = System.DateTime(2017, 12, 22))
| _ -> failwith "Unexpected"
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
type Foo = XmlProvider<...>

Full name: Tutorial.Foo
type XmlProvider

Full name: FSharp.Data.XmlProvider


<summary>Typed representation of a XML file.</summary>
       <param name='Sample'>Location of a XML sample file or a string containing a sample XML document.</param>
       <param name='SampleIsList'>If true, the children of the root in the sample document represent individual samples for the inference.</param>
       <param name='Global'>If true, the inference unifies all XML elements with the same name.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.xml'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans. The XmlProvider also infers string values as JSON.)</param>
       <param name='Schema'>Location of a schema file or a string containing xsd.</param>
type Foo' = obj

Full name: Tutorial.Foo'
type Foo'' = obj

Full name: Tutorial.Foo''
type TwoRoots = XmlProvider<...>

Full name: Tutorial.TwoRoots
val e1 : XmlProvider<...>.Choice

Full name: Tutorial.e1
XmlProvider<...>.Parse(text: string) : XmlProvider<...>.Choice


Parses the specified XML string
property XmlProvider<...>.Choice.Root1: Option<XmlProvider<...>.Root1>
property XmlProvider<...>.Choice.Root2: Option<XmlProvider<...>.Root2>
union case Option.Some: Value: 'T -> Option<'T>
val x : XmlProvider<...>.Root1
union case Option.None: Option<'T>
property XmlProvider<...>.Root1.Foo: string
property XmlProvider<...>.Root1.Fow: Option<int>
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val e2 : XmlProvider<...>.Choice

Full name: Tutorial.e2
val x : XmlProvider<...>.Root2
property XmlProvider<...>.Root2.Bar: string
property XmlProvider<...>.Root2.Baz: System.DateTime
namespace System
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
Fork me on GitHub