-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIO.cs
More file actions
71 lines (61 loc) · 2.34 KB
/
IO.cs
File metadata and controls
71 lines (61 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using System.Drawing;
namespace SharpOcarina
{
static class IO
{
public static void Export<T>(T Object, string Filename)
{
XmlWriterSettings XWS = new XmlWriterSettings();
XWS.NamespaceHandling = NamespaceHandling.OmitDuplicates;
XWS.NewLineChars = Environment.NewLine;
XWS.Indent = true;
//XmlSerializer XS = new XmlSerializer(Object.GetType());
var XS = new ConfigurationContainer().ConfigureType<ZScene>()
.EnableReferences(p => p.cloneid).Create();
StreamWriter SW = new StreamWriter(Filename);
XmlWriter XW = XmlWriter.Create(SW, XWS);
XW.WriteStartDocument();
XW.WriteComment("Created with " + Program.ApplicationTitle);
XS.Serialize(XW, Object);
XW.WriteEndDocument();
XW.Flush();
SW.Close();
}
public static T Import<T>(string Filename)
{
try
{
var XS = new ConfigurationContainer().ConfigureType<ZScene>()
.EnableReferences(p => p.cloneid).Create();
XmlReader XR = XmlReader.Create(Filename);
return (T)XS.Deserialize(XR);
}
catch (Exception e)
{
DebugConsole.WriteLine("Old XML detected, using old format to import it");
XmlSerializer XS = new XmlSerializer(typeof(T));
StreamReader SR = new StreamReader(Filename);
return (T)XS.Deserialize(SR);
}
}
/*
public static void BinExport<T>(T Object, string Filename)
{
Stream stream = File.Open(Filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, Object);
stream.Close();
}*/
}
}