這是我的計劃 -如何爲F#序列化獲得簡潔,清晰的XML輸出?
module Program
open System
open System.IO
open System.Text
open System.Xml
open System.Xml.Serialization
open System.Runtime.Serialization
open System.Reflection
open Microsoft.FSharp.Reflection
let getUnionTypes<'a>() =
let nestedTypes = typedefof<'a>.GetNestedTypes(BindingFlags.Public ||| BindingFlags.NonPublic)
Array.filter FSharpType.IsUnion nestedTypes
type Alpha =
{ X : int * int
Y : Alpha option }
type [<KnownType "GetTypes">] Beta =
| A of int * Alpha
| B of Beta option
| C of Map<int, Beta>
static member GetTypes() = getUnionTypes<Beta>()
let [<EntryPoint>] main _ =
let alpha = { X = (0, 0); Y = Some { X = (1, 1); Y = None }}
let betaA = A (0, alpha)
let betaB = B (Some betaA)
let betaC = C (Map.singleton 0 betaB)
let sb = new StringBuilder()
let xmlSerializer = DataContractSerializer(typeof<Beta>);
xmlSerializer.WriteObject(new XmlTextWriter(new StringWriter(sb)), betaC)
let sr = sb.ToString()
printfn "%A" sr
0
這裏的輸出 -
"<Program.Beta xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Progr
am.Beta.C" xmlns="http://schemas.datacontract.org/2004/07/"><item xmlns:d2p1="ht
tp://schemas.datacontract.org/2004/07/Microsoft.FSharp.Collections"><d2p1:serial
izedData xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.
Generic"><d3p1:KeyValuePairOfintProgram.BetalLTIrbuF><d3p1:key>0</d3p1:key><d3p1
:value i:type="Program.Beta.B"><item xmlns:d6p1="http://schemas.datacontract.org
/2004/07/Microsoft.FSharp.Core"><d6p1:value i:type="Program.Beta.A"><item1>0</it
em1><item2><X_x0040_ xmlns:d9p1="http://schemas.datacontract.org/2004/07/System"
><d9p1:m_Item1>0</d9p1:m_Item1><d9p1:m_Item2>0</d9p1:m_Item2></X_x0040_><Y_x0040
_><d6p1:value><X_x0040_ xmlns:d11p1="http://schemas.datacontract.org/2004/07/Sys
tem"><d11p1:m_Item1>1</d11p1:m_Item1><d11p1:m_Item2>1</d11p1:m_Item2></X_x0040_>
<Y_x0040_ i:nil="true" /></d6p1:value></Y_x0040_></item2></d6p1:value></item></d
3p1:value></d3p1:KeyValuePairOfintProgram.BetalLTIrbuF></d2p1:serializedData></i
tem></Program.Beta>"
我可能會做什麼簡單的事情,以改善這個XML輸出?我嘗試了各種屬性,但他們似乎都沒有幫助。請注意,我需要保持我的數據結構不可變,並且我希望避免編寫自己的序列化庫,或者如果可能的話,將依賴項添加到另一個自制的。它理想上應該是一個簡單的解決方案。
不要使用可識別聯合 – mydogisbox