2

When I try to instance a xmldocument object in f#, why do I get an error message?

open System.Xml
let doc = XmlDocument()

The error message is:

ip.fsx(5,11): error FS0039: The value or constructor 'XmlDocument' is not defined

Before I execute this code, I do

> #r "System.Xml";;
--> Referenced 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Xml.dll'

What do I wrong?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

-3

Try :

  let doc = new XmlDocument()

Here, new is a shorthand for calling a function associated with the type XmlDocument that constructs a value of the given type - these functions are called constructors. Not all F# and .NET types use constructors. (exerpt from Don Syme's book page 21.)

This is the reason that it is sometimes confusing where to put the new keyword.

For an explanation on where to use new keyword :

Community
  • 1
  • 1
FZed
  • 520
  • 3
  • 10
  • 3
    You don't need to use `new` keyword – Petr Feb 02 '15 at 15:04
  • @Petr : Most F# answers include new keyword ; see : http://stackoverflow.com/questions/332871/f-xml-parsing ; also used in Don Syme's "Expert F#" book page 209 of version 2. – FZed Feb 02 '15 at 16:00
  • 2
    It is misleading. The code from the question is working without `new` keyword – Petr Feb 02 '15 at 16:03
  • 1
    Yes you may use new keyword to create objects but you don't have to. And it is not the cause of the problem in question. – Petr Feb 02 '15 at 17:38