Push Alpha Version
This commit is contained in:
127
uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs
Normal file
127
uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace InfineonHMI.Common
|
||||
{
|
||||
public static class L4ItXmlSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes an object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="serializableObject"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="encrypt"></param>
|
||||
/// <param name="rootElementName"></param>
|
||||
|
||||
public static void SerializeObject<T>(T serializableObject, string fileName, bool encrypt = false, string rootElementName = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileName))
|
||||
return;
|
||||
|
||||
if (serializableObject == null)
|
||||
return;
|
||||
|
||||
XmlSerializer serializer;
|
||||
if (rootElementName != null)
|
||||
{
|
||||
var xmlRoot = new XmlRootAttribute(rootElementName);
|
||||
serializer = new XmlSerializer(serializableObject.GetType(), xmlRoot);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer = new XmlSerializer(serializableObject.GetType());
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var dir = new FileInfo(fileName).DirectoryName;
|
||||
if (dir != null && !Directory.Exists(dir)) // Überprüfen Sie, ob dir nicht null ist, bevor Sie es verwenden
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
var xmlDocument = new XmlDocument();
|
||||
using var stream = new MemoryStream();
|
||||
serializer.Serialize(stream, serializableObject);
|
||||
stream.Position = 0;
|
||||
xmlDocument.Load(stream);
|
||||
if (encrypt && false)
|
||||
{
|
||||
//FileEncryption.SaveEncryptedToFile(fileName, xmlDocument.OuterXml);
|
||||
return;
|
||||
}
|
||||
xmlDocument.Save(fileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Write(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes an xml file into an object list
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="decrypt"></param>
|
||||
/// <param name="rootElementName"></param>
|
||||
/// <returns></returns>
|
||||
///
|
||||
///
|
||||
public static T DeSerializeObject<T>(string fileName, bool decrypt = false, string rootElementName = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName)) return default!;
|
||||
|
||||
T objectOut;
|
||||
|
||||
try
|
||||
{
|
||||
string xmlString;
|
||||
if (decrypt && false)
|
||||
{
|
||||
//xmlString = FileEncryption.ReadDecryptedFromFile(fileName)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
var xmlDocument = new XmlDocument();
|
||||
xmlDocument.Load(fileName);
|
||||
xmlString = xmlDocument.OuterXml;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(xmlString))
|
||||
{
|
||||
// Handle empty xmlString if necessary
|
||||
return default!;
|
||||
}
|
||||
|
||||
using var read = new StringReader(xmlString);
|
||||
var outType = typeof(T);
|
||||
|
||||
XmlSerializer serializer;
|
||||
if (rootElementName != null)
|
||||
{
|
||||
var root = new XmlRootAttribute(rootElementName);
|
||||
serializer = new XmlSerializer(outType, root);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer = new XmlSerializer(outType);
|
||||
}
|
||||
|
||||
using XmlReader reader = new XmlTextReader(read);
|
||||
objectOut = (T)serializer.Deserialize(reader)!;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Write(ex);
|
||||
return default!;
|
||||
}
|
||||
|
||||
return objectOut;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user