Serialize exploration Attempt
Key points to be considered at the beginning of the design are:
1. Does the data structure support Web Service?
2. Whether to support persistence objects in an XML structure and not conflict with Contract persistence is a supplement
3. When the. net Object breaks down the remote boundary, consider supporting runtime persistence.
4. Avoid using XML and Runtime technologies when only common persistence is required.
XML Serial
After using Dotnet for so long, I have hardly used Serial technology.
Open Systemopen System. IO [<CLIMutable>] // This feature must be added here, so that this Recode has a default constructor type DateSerTest = {Local: DateTime; Utc: dateTime} let ser = new System. xml. serialization. xmlSerializer (typeof <DateSerTest>) let d = {Local = DateTime. specifyKind (new DateTime (2014, 10, 1), DateTimeKind. local) Utc = DateTime. specifyKind (new DateTime (2014, 10, 1), DateTimeKind. utc)} let testSerialization (dt: DateSerTest) = use MS = New IO. memoryStream () ser. serialized (MS, o = dt) let result = Text. encoding. default. getString (ms. getBuffer () ms. close () resultlet objecblock = testSerialization d // get the Xml byte stream from the memory stream. The stream is the data is the media, and the Serializer is the template. Let MS = new IO. MemoryStream (Text. Encoding. Unicode. GetBytes (objecblock) let newd = ser. Deserialize (MS):?> DateSerTest // generate the object if d = newd then prrintfn "Equal"
Modify the memory stream to a file stream, and then perform the following test to get the same result.
let testFileSerialization(dt:DateSerTest) = let filename = "serialization.xml" use ms = new StreamWriter(filename) ser.Serialize(ms, o = dt) ms.Close()testFileSerialization dlet ms = new StreamReader("serialization.xml")let newd = ser.Deserialize(ms) :?> DateSerTestif d = newd then printfn "Equal"
Classes can be more complex. In special cases, you need to add specific attributes for special definitions. Above