Update Xml with CS

This commit is contained in:
2021-05-11 17:59:27 +08:00
parent 982b79bbb0
commit fc3f3bec48
24 changed files with 310 additions and 470 deletions

View File

@@ -1,4 +1,5 @@
using Furion;
using System;
using System.Collections.Generic;
using System.IO;
@@ -15,12 +16,24 @@ namespace Ewide.Core.Util
{
public class XmlSerializerUtil
{
static string[] str = AppDomain.CurrentDomain.BaseDirectory.Split("ewide_core");
static string path = str[0]+ @"ewide_core\Api\Ewide.Core\SeedDataXml\";
/// <summary>
/// XML 要读取的路径
/// </summary>
private string DirPath { set; get; }
/// <summary>
/// 当前程序集
/// </summary>
private string currentAssemblyName { get { return "Ewide.Core"; } }
/// <summary>
/// 读取文件夹下所有XML
/// add-migration init -c defaultDbContext 备用数据
/// </summary>
/// <returns></returns>
public Dictionary<Type, object> ReaderALL()
{
string dirPath = CurrentDirPath();
Dictionary<Type, object> dic = new Dictionary<Type, object>();
DirectoryInfo folder = new DirectoryInfo(path);
DirectoryInfo folder = new DirectoryInfo(dirPath);
foreach (FileInfo file in folder.GetFiles())
{
//除去.xml 后缀
@@ -34,16 +47,12 @@ namespace Ewide.Core.Util
}
return dic;
}
private object Deserialize<T>(string xmlContent, Type[] Arrtype)
{
object obj = null;
using (StringReader sr = new StringReader(xmlContent))
{
XmlSerializer xmldes = new XmlSerializer(typeof(List<T>), Arrtype);
obj = xmldes.Deserialize(sr);
}
return obj;
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type"></param>
/// <param name="xmlContent">XML内容</param>
/// <returns></returns>
private object Deserialize(Type type, string xmlContent)
{
object obj = null;
@@ -54,20 +63,32 @@ namespace Ewide.Core.Util
}
return obj;
}
public string StreamRead(string path)
/// <summary>
/// 数据流读取
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private string StreamRead(string path)
{
using (StreamReader reader = new StreamReader(path, Encoding.Default))
{
return reader.ReadToEnd();
}
}
/// <summary>
/// 反射 根据类名 反射整个类 得到实例
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private object AppInfo(string name)
{
string assemblyName = "Ewide.Core";
string nameSpace = "Ewide.Core";
string fullName = nameSpace + "." + name;
object ect = Assembly.Load(assemblyName).CreateInstance(fullName);
return ect;
if (name == "BsHouseProjectInfo") //BsHouseProjectInfo 这个类 写在 application 程序集
{
string fullNamei = string.Join('.', "Ewide.Application.Entity", name);
return Assembly.Load("Ewide.Application").CreateInstance(fullNamei);
}
string fullName = string.Join('.', currentAssemblyName, name);
return Assembly.Load(currentAssemblyName).CreateInstance(fullName);
}
/// <summary>
@@ -78,13 +99,72 @@ namespace Ewide.Core.Util
/// <param name="className">XML名称</param>
public void WriteXML(Type type, object data, string className)
{
string writePath = path + className + ".xml";
string writePath = CurrentDirPath(className);
using (FileStream fs = new FileStream(writePath, FileMode.OpenOrCreate))
{
XmlSerializer ser = new XmlSerializer(type);
ser.Serialize(fs, data);
}
}
/// <summary>
/// 要操作的文件夹目录
/// </summary>
/// <returns></returns>
private string CurrentDirPath()
{
// 在for 循环里, 不用每次都读取 下面的代码
if (DirPath != null)
return DirPath;
string targetPath = String.Empty;
string appConfigPath = App.Configuration["WriteXmlPath:ReadSelect"] == "SeedData" ? App.Configuration["WriteXmlPath:SeedData"] : App.Configuration["WriteXmlPath:DataBase"];
string path = Directory.GetCurrentDirectory();
DirectoryInfo dirInfo = new DirectoryInfo(path);
foreach (DirectoryInfo item in dirInfo.Parent.GetDirectories())
{
if (item.Name == currentAssemblyName)
{
targetPath = item.GetDirectories(appConfigPath).FirstOrDefault().FullName;
break;
}
}
DirPath = targetPath;
return targetPath;
}
/// <summary>
/// 根据类名 创建一个XML文件 写入数据
/// </summary>
/// <param name="className"></param>
/// <returns></returns>
private string CurrentDirPath(string className)
{
string path = CurrentDirPath();
return string.Join(@"\", path, className + ".xml");
}
/// <summary>
/// 写入种子数据 --不常用
/// </summary>
public void WriteDataSeed()
{
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Contains("Ewide.Core"));
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
//if (type.Name.EndsWith("SeedData"))
if(type.Name.EndsWith("SysEmpExtOrgPos"))
{
object obHelper = Activator.CreateInstance(type);
MethodInfo methodinfo = type.GetMethod("HasData");
object objs = methodinfo.Invoke(obHelper, new object[] { null, null });
Type objType = objs.GetType();
//if (objType.Name == "SysEmpExtOrgPos[]" || objType.Name == "SysConfig[]" || objType.Name == "SysDictData[]" || objType.Name == "SysDictType[]" || objType.Name == "SysEmpPos[]" || objType.Name == "SysEmp[]" || objType.Name == "SysMenu[]" || objType.Name == "SysOrg[]" || objType.Name == "SysPos[]" || objType.Name == "SysRole[]" || objType.Name == "SysTenant[]" || objType.Name == "SysTimer[]" || objType.Name == "SysUser[]")
if (objType.Name == "SysEmpExtOrgPos[]")
{
string className = objType.Name.Remove(objType.Name.Length - 2, 2);
WriteXML(objType, objs, className);
}
}
}
}
}
}