2016-01-26 75 views
1

我試圖將兩個XmlDocument.xml用於WebAPI 2.0文檔。但是當我執行下面的代碼時,它會返回< /doc>而不會出錯。看起來我一切正常,所以我有點困惑。 printf聲明在哪裏,這是失敗的結果值。代碼似乎在完美工作之前和之後。XML類型提供程序創建新的XML對象失敗

#r "../packages/FSharp.Data.2.2.5/lib/net40/FSharp.Data.dll" 
#r "System.Xml.Linq.dll" 
open FSharp.Data 
open System.IO 

// Define your library scripting code here 

let seedPath = __SOURCE_DIRECTORY__ + "../../FieldOps/bin" 
let outFile = __SOURCE_DIRECTORY__ + "../../FieldOps/App_Data/XmlDocument.xml" 

type XmlDocument = XmlProvider<"""<?xml version="1.0"?> 
<doc> 
    <assembly><name>lala</name></assembly> 
    <members> 
     <member name=""> 
      <summary>lala</summary> 
      <param name="">lala</param> 
      <param name="">lala</param> 
      <returns>lala</returns> 
     </member> 
     <member name=""></member> 
    </members> 
</doc> 
"""> 

let xmlPaths = 
    [ 
     "../bin/Release" 
    ] 

let seed = XmlDocument.Load(Path.Combine(seedPath, "XmlDocument.xml")) 
let addFile path = Path.Combine(path, "XmlDocument.XML") 
let loadXmlDoc (path: string) = XmlDocument.Load(path) 
let joinXmlDocuments (acc: XmlDocument.Doc) path = 
    let doc = (addFile >> loadXmlDoc) path 
    let members = Array.append acc.Members doc.Members 
    let result = new XmlDocument.Doc(acc.Assembly, members) 
    printf "result ------------------- %A --------------------" result 
    result 

let joinedDocs = 
    xmlPaths 
    |> List.fold (joinXmlDocuments) seed 

let xml = joinedDocs.XElement.ToString() 
File.WriteAllText(outFile, xml) 
+0

有什麼錯誤? –

+0

沒有任何錯誤。它只是用'< /doc>'來代替所有數據。 – Jon49

回答

1

更新 →我想我知道爲什麼它有問題。可能是因爲它們是不可變的,所以你需要創建一個新的。但是,如何輕鬆做到這一點,我不知道。

看起來XmlProvider不能識別它自己的類型。不確定到底發生了什麼。但是,當我從頭開始重新創建類型時,它不會返回我看到的空值< /doc>

這裏是我落得這樣做:

let assembly = new XmlDocument.Assembly(acc.Assembly.Name) 
let members = 
    Array.append acc.Members doc.Members 
    |> Array.map 
     (
      fun x -> 
      let parameters = 
       x.Params 
       |> Array.map (fun x -> new XmlDocument.Param(x.Name, x.Value)) 
      new XmlDocument.Member(x.Name, x.Summary, parameters, x.Returns) 
     ) 
let result = XmlDocument.Doc(assembly, members) 
1

這是固定的F#數據2.3.0-β1