367 lines
13 KiB
C#
367 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
using AppRes = Eugen.ESystem.IO.Properties.AppResources;
|
|
|
|
namespace Eugen.ESystem.IO
|
|
{
|
|
public class SaveXMLFile
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
static string copyright = GenerateCopyright();
|
|
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
static string date = value.ToShortDateString();
|
|
static string time = value.ToLongTimeString();
|
|
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
|
|
if (startYear.Contains("-"))
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf("-"));
|
|
}
|
|
else
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
}
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
string start = temp.Remove(temp.IndexOf(startYear) - 1);
|
|
string end = (temp.Remove(0, temp.IndexOf(" by")));
|
|
|
|
return String.Concat(start, " ", startYear, "-", currentYear, end);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the dll file.
|
|
/// </summary>
|
|
public static string DllName { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the dll file.
|
|
/// </summary>
|
|
public static string DllVersion { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice.
|
|
/// </summary>
|
|
public static string Copyright { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the name-, version- and copyright-information of the dll file.
|
|
/// </summary>
|
|
public static string[] DllInfo { private set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
|
|
string[] dllInfo = new string[3];
|
|
dllInfo[0] = dllName;
|
|
dllInfo[1] = dllVersion;
|
|
dllInfo[2] = copyright;
|
|
DllInfo = dllInfo;
|
|
}
|
|
|
|
protected string ProgramName { private set; get; }
|
|
protected string ProgramVersion { private set; get; }
|
|
protected string ProgramCopyright { private set; get; }
|
|
#endregion
|
|
|
|
static SaveXMLFile()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public SaveXMLFile()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public enum multigroupFile
|
|
{
|
|
No,
|
|
Yes
|
|
}
|
|
|
|
public string FileName { get; set; }
|
|
public multigroupFile MultiGroupFile { get; set; }
|
|
public string Topic = ""; //z.B. "NX-Portal"
|
|
public string Group { get; set; } //z.B. "NX"
|
|
public string Key { get; set; } //z.B. "Key"
|
|
public string[] Values { get; set; } //z.B. "NX 1953"
|
|
public string[] Standard { get; set; } //"yes" oder "no"
|
|
public int StandardNr { get; set; } //z.B. "2"
|
|
|
|
private void SaveSingleGroupFile()
|
|
{
|
|
//Namensraum definieren
|
|
XNamespace ns = "https://www.engelglobal.com/de/at/index.html";
|
|
|
|
//Wurzelelement erzeugen
|
|
XElement rootElement = new XElement(ns + Topic);
|
|
|
|
//Namensraum als Attribut angeben
|
|
rootElement.Add(new XAttribute("xmlns", ns));
|
|
|
|
//Der Wert
|
|
for (int id = 0; id < Values.Length; id++)
|
|
{
|
|
XElement groupElement = new XElement(ns + Group);
|
|
groupElement.Add(new XAttribute("Id", id + 1));
|
|
groupElement.Add(new XAttribute(Key, Values[id]));
|
|
if (StandardNr == id + 1)
|
|
{
|
|
groupElement.Add(new XAttribute("Standard", "yes"));
|
|
}
|
|
else
|
|
{
|
|
groupElement.Add(new XAttribute("Standard", "no"));
|
|
}
|
|
|
|
rootElement.Add(groupElement);
|
|
}
|
|
|
|
rootElement.Save(FileName);
|
|
|
|
MessageBox.Show(AppRes.msgBoxTxt001, AppRes.msgBoxCapt001, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
private void SaveMultiGroupFile(string[,] values)
|
|
{
|
|
//Namensraum definieren
|
|
XNamespace ns = "https://www.engelglobal.com/de/at/index.html";
|
|
|
|
//Wurzelelement erzeugen
|
|
XElement rootElement = new XElement(ns + Topic);
|
|
|
|
//Namensraum als Attribut angeben
|
|
rootElement.Add(new XAttribute("xmlns", ns));
|
|
|
|
//Der Wert
|
|
for (int id = 0; id < values.Length / 5; id++)
|
|
{
|
|
XElement groupElement = new XElement(ns + values[id, 0]);
|
|
groupElement.Add(new XAttribute("Id", values[id, 1]));
|
|
groupElement.Add(new XAttribute(values[id, 2], values[id, 3]));
|
|
groupElement.Add(new XAttribute("Standard", values[id, 4]));
|
|
|
|
rootElement.Add(groupElement);
|
|
}
|
|
|
|
rootElement.Save(FileName);
|
|
|
|
MessageBox.Show(AppRes.msgBoxTxt001, AppRes.msgBoxCapt001, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
if (MultiGroupFile == multigroupFile.Yes)
|
|
{
|
|
//XML in collecton laden
|
|
var loadXML = new LoadXMLFile();
|
|
loadXML.FileName = FileName;
|
|
loadXML.Topic = Topic;
|
|
loadXML.Group = Group;
|
|
loadXML.Key = Key;
|
|
loadXML.LoadAllValues = LoadXMLFile.LoadAll.Yes;
|
|
loadXML.Load();
|
|
string[,] collection = loadXML.Values;
|
|
|
|
bool exist = false;
|
|
if (loadXML.Values != null)
|
|
{
|
|
List<List<string>> lafter = new List<List<string>>();
|
|
List<List<string>> lmiddle = new List<List<string>>();
|
|
List<List<string>> lbefore = new List<List<string>>();
|
|
|
|
for (int i = 0; i < loadXML.Values.Length / 5; i++)
|
|
{
|
|
if (loadXML.Values[i, 0] == Group)
|
|
{
|
|
exist = true;
|
|
//Die alten Werte igrorieren, weil diese durch die neuen Werte ersetzt und nachher in die 'Gruppe_mitte' aufgenommen werden
|
|
}
|
|
else
|
|
{
|
|
if (exist)
|
|
{
|
|
//Die Werte in die 'Gruppe_danach' aufnehmen
|
|
List<string> iafter = new List<string>();
|
|
|
|
iafter.Add(loadXML.Values[i, 0]);
|
|
iafter.Add(loadXML.Values[i, 1]);
|
|
iafter.Add(loadXML.Values[i, 2]);
|
|
iafter.Add(loadXML.Values[i, 3]);
|
|
iafter.Add(loadXML.Values[i, 4]);
|
|
|
|
lafter.Add(iafter);
|
|
}
|
|
else
|
|
{
|
|
//Die Werte in die 'Gruppe_davor' aufnehmen
|
|
List<string> ibefore = new List<string>();
|
|
|
|
ibefore.Add(loadXML.Values[i, 0]);
|
|
ibefore.Add(loadXML.Values[i, 1]);
|
|
ibefore.Add(loadXML.Values[i, 2]);
|
|
ibefore.Add(loadXML.Values[i, 3]);
|
|
ibefore.Add(loadXML.Values[i, 4]);
|
|
|
|
lbefore.Add(ibefore);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Die Werte in die 'Gruppe_mitte' aufnehmen
|
|
for (int j = 0; j < Values.Length; j++)
|
|
{
|
|
List<string> imiddle = new List<string>();
|
|
|
|
imiddle.Add(Group); //z.B.: 'NX'
|
|
imiddle.Add((j + 1).ToString()); //Id, z.B.: '2'
|
|
imiddle.Add(Key); //z.B.: 'Version'
|
|
imiddle.Add(Values[j]); //z.B.: 'NX 2312'
|
|
imiddle.Add(Standard[j]); //Standard, z.B.: 'yes'
|
|
|
|
lmiddle.Add(imiddle);
|
|
}
|
|
|
|
//Gruppe_davor + Gruppe_mitte + Gruppe_danach
|
|
|
|
//Gesamtlänge ermitteln
|
|
int cbefore = lbefore.Count;
|
|
int cmiddle = lmiddle.Count;
|
|
int cafter = lafter.Count;
|
|
string[,] values = new string[lbefore.Count + cmiddle + lafter.Count, 5];
|
|
|
|
//'Gruppe_davor' einfügen
|
|
if (lbefore.Count > 0)
|
|
{
|
|
for (int i = 0; i < lbefore.Count; i++)
|
|
{
|
|
int j = 0;
|
|
foreach (var item in lbefore[i])
|
|
{
|
|
values[i, j] = item;
|
|
j++;
|
|
}
|
|
}
|
|
}
|
|
|
|
//TODO:
|
|
//'Gruppe_mitte' einfügen
|
|
if (cmiddle > 0)
|
|
{
|
|
for (int i = lbefore.Count; i < lbefore.Count + cmiddle; i++)
|
|
{
|
|
int j = 0;
|
|
foreach (var item in lmiddle[i])
|
|
{
|
|
values[i, j] = item;
|
|
j++;
|
|
}
|
|
}
|
|
}
|
|
|
|
//'Gruppe_danach' einfügen
|
|
if (lafter.Count > 0)
|
|
{
|
|
int i = lbefore.Count + cmiddle;
|
|
foreach (var itema in lafter)
|
|
{
|
|
int j = 0;
|
|
foreach (var item in itema)
|
|
{
|
|
values[i, j] = item;
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
//Speichern
|
|
SaveMultiGroupFile(values);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//Speichern
|
|
SaveSingleGroupFile();
|
|
}
|
|
}
|
|
|
|
private string[,] LoadXML()
|
|
{
|
|
var load = new LoadXMLFile();
|
|
load.FileName = FileName;
|
|
load.Topic = Topic;
|
|
load.Group = Group;
|
|
load.Key = Key;
|
|
load.LoadAllValues = LoadXMLFile.LoadAll.Yes; //Es werden alle Elemente geladen
|
|
load.Load();
|
|
|
|
return load.Values;
|
|
}
|
|
}
|
|
}
|