123 lines
3.7 KiB
C#
123 lines
3.7 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using Formatting = System.Xml.Formatting;
|
|
|
|
namespace DB.Recipe
|
|
{
|
|
public abstract class Recipe
|
|
{
|
|
static string m_selectedRecipeFile;
|
|
public static string SelectedRecipeFile {
|
|
get { return m_selectedRecipeFile; }
|
|
set { m_selectedRecipeFile = value; }
|
|
}
|
|
|
|
static bool m_isDirty = false;
|
|
public static bool IsDirty {
|
|
get { return m_isDirty; }
|
|
set { m_isDirty = value; }
|
|
}
|
|
|
|
#region 추상함수
|
|
public abstract void Save(XmlWriter wr, bool includeModel = true);
|
|
public abstract void Load(XmlElement root);
|
|
public abstract string Save();
|
|
public abstract void SubLoad(string fileName);
|
|
|
|
// Data ReadOnly
|
|
public abstract void LoadReadOnly(XmlElement root);
|
|
#endregion
|
|
|
|
public Recipe()
|
|
{
|
|
recipeList.Add(this);
|
|
}
|
|
|
|
public List<Recipe> recipeList = new List<Recipe>();
|
|
|
|
public void Load(string fileName)
|
|
{
|
|
try {
|
|
XmlDocument doc = new XmlDocument();
|
|
var extenstion = Path.GetExtension(fileName);
|
|
if (extenstion == ".json") {
|
|
|
|
var jsonString = File.ReadAllText(fileName);
|
|
|
|
doc = JsonConvert.DeserializeXmlNode(jsonString, "RecipeBody");
|
|
|
|
} else {
|
|
FileInfo fi = new FileInfo(fileName);
|
|
StreamReader sr = new StreamReader(fileName);
|
|
doc.Load(sr);
|
|
sr.Dispose();
|
|
}
|
|
|
|
XmlElement root = doc.DocumentElement;
|
|
|
|
foreach (XmlElement childnode in root.ChildNodes) {
|
|
foreach (Recipe recipe in recipeList) {
|
|
if (childnode.Name == recipe.GetType().Name) {
|
|
recipe.Load(childnode);
|
|
}
|
|
}
|
|
}
|
|
SelectedRecipeFile = fileName;
|
|
} catch (Exception ex) {
|
|
throw ex;
|
|
}
|
|
}
|
|
public void Load()
|
|
{
|
|
Load(SelectedRecipeFile);
|
|
}
|
|
|
|
public void Save(string fileName, bool includeModel = true)
|
|
{
|
|
try {
|
|
#if false
|
|
XmlTextWriter wr = new XmlTextWriter(fileName, null);
|
|
|
|
wr.Formatting = Formatting.Indented;
|
|
wr.WriteStartElement("RecipeBody");
|
|
|
|
foreach (Recipe recipe in recipeList)
|
|
{
|
|
wr.WriteStartElement(recipe.GetType().Name);
|
|
recipe.Save(wr);
|
|
wr.WriteEndElement();
|
|
}
|
|
|
|
wr.WriteEndElement();
|
|
wr.Close();
|
|
#else
|
|
using (MemoryStream stream = new MemoryStream()) {
|
|
XmlTextWriter wr = new XmlTextWriter(stream, Encoding.UTF8);
|
|
wr.Formatting = Formatting.Indented;
|
|
|
|
foreach (Recipe recipe in recipeList) {
|
|
wr.WriteStartElement(recipe.GetType().Name);
|
|
recipe.Save(wr, includeModel);
|
|
wr.WriteEndElement();
|
|
}
|
|
wr.Flush();
|
|
|
|
XmlDocument doc = new XmlDocument();
|
|
stream.Position = 0;
|
|
doc.Load(stream);
|
|
|
|
string jsonStr = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
|
|
File.WriteAllText(fileName, jsonStr);
|
|
}
|
|
#endif
|
|
} catch (Exception ex) {
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|