This function takes too many arguments, or is used in a context where a function is not expectedProgram
This function takes too many arguments, or is used in a context where a function is not expectedProgram
This is my *first* F# program converted from Visual Basic. This program is made to only copy the XML file from one to another.
open System.IO
open System.Linq
open System.Xml
open System.Xml.Linq
open System.Text
let CreateXDocument(inputPath:string)=
let rs = XmlReaderSettings()
rs.IgnoreProcessingInstructions <- true
rs.IgnoreComments <- true
rs.ConformanceLevel <- ConformanceLevel.Document
let xr = XmlReader.Create(inputPath, rs) in
let xd = XDocument.Load(xr)
xd;;
let CreateStreamWiter(outputPath:string)=
let sw:StreamWriter = new StreamWriter(outputPath, false, Encoding.GetEncoding("UTF-8"))
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
sw;;
let XmlTraverse(xd:XDocument, sw:StreamWriter)=
let nodes:seq<'XNodes>=xd.Nodes
for node in nodes do
ProcessNode(node, sw)
;;
let ProcessNode(node:XNode, sw:StreamWriter) =
match node with
| XElement elem -> let elementName:string = elem.Name.LocalName
let attrList:seq<'XAttribute>=elem.Attributes
StartElement sw elementName attrList
for childNode in elem.Nodes do
ProcessNode childNode sw
EndElement sw elementName
| XText text -> let value = text.Value
OutText sw value
| XWhitespace ws -> let value = ws.Value
OutText sw value
| _ -> ();;
let StartElement(sw:StreamWriter, elementName:string, attributes:seq<'XAttribute>)=
sw.Write("<" + elementName)
for attr in attributes do
sw.Write(" {0}=\"{1}\"", attr.name, attr.value)
sw.Write(">");;
let EndElement(sw:StreamWriter, elementName:string)=
sw.Write("</" + elementName + ">");;
let OutText(sw:StreamWriter, text:string)=
sw.Write(text);;
[<EntryPoint>]
let main argv =
printfn "%A" argv
let xd=CreateXDocument("People.xml")
let sw=CreateStreamWiter("People_Copy.xml")
XmlTraverse(xd, sw)
sw.Close()
0 However Visual Studio 2012 displays following error message.
Error 1This function takes too many arguments, or is used in a context where a function is not expectedProgram.fs 22 32FSharpBasic
Error 2The value or constructor 'ProcessNode' is not definedProgram.fs 24 13FSharpBasic
Error 3The pattern discriminator 'XElement' is not definedProgram.fs 29 7FSharpBasic
Error 4Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.Program.fs 44 38FSharpBasic
Error 5Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.Program.fs 44 49FSharpBasic
As I am new to F# I could not remove these messages. Can you help me?
1) XDocument.Nodes is a method rather than a property, so write
let XmlTraverse(xd:XDocument, sw:StreamWriter)=
let nodes = xd.Nodes()
for node in nodes do
ProcessNode(node, sw)
instead (I've also dropped the redundant type declaration and concluding ;; punctuation) -- or, even more tersely
let XmlTraverse(xd:XDocument, sw:StreamWriter)=
xd.Nodes() |> Seq.iter (fun node -> ProcessNode(node, sw))or, if you define ProcessNode with the arguments in the other order and not as a tuple
let ProcessNode (sw:StreamWriter) (node:XNode) =
...
let XmlTraverse(xd:XDocument, sw:StreamWriter)=
xd.Nodes() |> Seq.iter (ProcessNode sw)
2) F# doesn't do forward references -- you have to define ProcessNode earlier in the file than XmlTraverse as in the above
3) The match-on-type syntax looks like this
| ?: XElement as elem ->