FSI缓存不需要的东西?

时间:2015-05-28 03:52:21

标签: xml f# f#-interactive

我在F#中有一些代码来验证针对模式的XML。代码如下:

module IA.HelperScripts.ValidateXmlSchema
open System.IO
open System.Xml
open System.Xml.Schema

let dictionary = __SOURCE_DIRECTORY__
let solutionPath = dictionary.Substring(0, dictionary.LastIndexOf('\\', dictionary.LastIndexOf('\\') - 1))
let schemaFolder = solutionPath + "\\Schemas"
let errorMessages = new System.Text.StringBuilder()
let schemas = new XmlSchemaSet()

let LoadSchema (schemaPath : string) (schemas : XmlSchemaSet) =
    use stream = new StreamReader(schemaPath)
    use xmlReader = XmlReader.Create(stream)
    let schema = XmlSchema.Read(xmlReader, null)
    schemas.Add(schema)

let Validate (xmlPath : string) =
    for schemaPath in Directory.GetFiles(schemaFolder) do
        LoadSchema schemaPath schemas |> ignore

    let settings = new XmlReaderSettings()
    settings.Schemas <- schemas
    settings.ValidationType <- ValidationType.Schema
    settings.ValidationEventHandler.AddHandler(fun o (e: ValidationEventArgs) -> 
        errorMessages.AppendFormat("{0} at position {1} of line {2}.", e.Message, e.Exception.LinePosition, e.Exception.LineNumber).AppendLine() |> ignore)

    use xmlStream = new StreamReader(xmlPath)
    use xmlReader = XmlReader.Create(xmlStream, settings)
    let document = new XmlDocument()
    document.Load(xmlReader) |> ignore

    let result = errorMessages.ToString()

    match result with
    | r when r.Length > 0 -> printfn "Error: \r\n%s" result
    | _ -> printfn "Validation Passed"

我有另一个fsx文件加载到fs文件之上并执行validate函数。代码如下:

#load "ValidateXmlSchema.fs"
open System.Reflection
open System.Collections.Generic


fsi.ShowDeclarationValues <- false

IA.HelperScripts.ValidateXmlSchema.Validate @"D:\t\IA\XmlForValidation.xml"

当我选择全部和Alt + Enter时,每次都可以正常工作。第一次运行所有脚本文件后,我只选择最后一行调用验证函数,它失败并出现以下错误:

  

System.Xml.Schema.XmlSchemaValidationException:已声明全局元素“http://tempuri.org/BaseSchema:PartnerFeed”。      在System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(XmlSchemaValidationException e,XmlSeverityType严重性)      在System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(XmlSchemaException e)      在System.Xml.Schema.XmlSchemaValidator.RecompileSchemaSet()      在System.Xml.Schema.XmlSchemaValidator.Init()      在System.Xml.Schema.XmlSchemaValidator..ctor(XmlNameTable nameTable,XmlSchemaSet架构,IXmlNamespaceResolver namespaceResolver,XmlSchemaValidationFlags validationFlags)      在System.Xml.XsdValidatingReader.SetupValidator(XmlReaderSettings readerSettings,XmlReader reader,XmlSchemaObject partialValidationType)      在System.Xml.XsdValidatingReader..ctor(XmlReader reader,XmlResolver xmlResolver,XmlReaderSettings readerSettings,XmlSchemaObject partialValidationType)      在System.Xml.XmlReaderSettings.AddValidation(XmlReader reader)      在System.Xml.XmlReaderSettings.CreateReader(TextReader输入,String baseUriString,XmlParserContext inputContext)      在System.Xml.XmlReader.Create(TextReader输入,XmlReaderSettings设置,String baseUri)      位于D:\ ECOVSO \ KSP \ Dev \ KSP \ Tools \ HelperScripts \ ValidateXmlSchema.fs中的FSI_0004.IA.HelperScripts.ValidateXmlSchema.Validate(String xmlPath):第28行      at。$ FSI_0006.main @()   因错误而停止

我认为错误可能是由FSI会话缓存的,然后发现XmlDocument有重复的根元素。但实际上我通过“use”声明了XmlStream和XmlReader。请帮我弄清楚为什么我必须重置交互式会话或重新运行所有脚本才能使该功能正常工作。

1 个答案:

答案 0 :(得分:0)

每次拨打Validate时,都会调用LoadSchema,这会将架构添加到schemas。因此,如果您拨打Validate两次,schemas将最终包含同一架构的两个副本。难怪会导致错误。