using System; namespace xml { public static class XMLer { static readonly string strAttributeDataType = "datatype"; static readonly string strAttributeName = "name"; static readonly string strTypeNull = "null"; static readonly string strTypePrimitive = "primitive"; static readonly string strTypeArray = "array"; static readonly string strTypeClass = "class"; static class Raw { public static bool IsRawableType(Type type) { if (type.Equals(typeof(float)) || type.Equals(typeof(double)) || type.Equals(typeof(decimal))) { return true; } return false; } static bool IsXDigit(char c) { if ('0' <= c && c <= '9') return true; if ('a' <= c && c <= 'f') return true; if ('A' <= c && c <= 'F') return true; return false; } static byte[] hexStringToBytes(string str) { do { if (str.Length % 2 != 0) { break; } foreach (char c in str) { if (!IsXDigit(c)) { break; } } byte[] ret = new byte[str.Length / 2]; for (int i = 0; i < str.Length; i+=2) { string temp = str.Substring(i, 2); ret[i / 2] = byte.Parse(temp, System.Globalization.NumberStyles.HexNumber); } return ret; } while (false); throw new ArgumentException("Ungültiger Hex-String."); } public static Object FromString(string str, Type type) { Object ret; bool raw = false; if (str.StartsWith("R", StringComparison.Ordinal)) { raw = true; str = str.Remove(0, 1); } if (type.Equals(typeof(float))) { if (raw) { ret = BitConverter.ToSingle(hexStringToBytes(str), 0); } else { ret = float.Parse(str); } } else if (type.Equals(typeof(double))) { if (raw) { ret = BitConverter.ToDouble(hexStringToBytes(str), 0); } else { ret = double.Parse(str); } } else if (type.Equals(typeof(decimal))) { if (raw) { string[] strings = str.Split(','); if (strings.Length != 4) { throw new Exception("Ungültiger Rawwert für decimal."); } ret = new decimal(new int[] { int.Parse(strings[0]), int.Parse(strings[1]), int.Parse(strings[2]), int.Parse(strings[3]) }); } else { ret = decimal.Parse(str); } } else { throw new ArgumentException("Invalid Type \"" + type.FullName + "\" given."); } return ret; } public static string ToString(Object o) { string ret; Type type = o.GetType(); if (type == typeof(float)) { ret = "R" + BytesToString(BitConverter.GetBytes((float)o)); } else if (type == typeof(double)) { ret = "R" + BytesToString(BitConverter.GetBytes((double)o)); } else if (type == typeof(decimal)) { int[] ints = decimal.GetBits((decimal)o); ret = "R" + ints[0].ToString() + "," + ints[0].ToString() + "," + ints[0].ToString() + "," + ints[0].ToString(); } else { throw new ArgumentException("Invalid Type \"" + o.GetType().FullName + "\" given."); } return ret; } static string BytesToString(byte[] bytes) { string ret = ""; foreach (byte b in bytes) { ret += b.ToString("X2"); } return ret; } } static System.Xml.XmlElement ObjectToXML(Object o, string name, System.Xml.XmlDocument xdoc) { Type type = o != null ? o.GetType() : null; string typename; if (o == null) { typename = "null"; } else if (type.IsPrimitive) { typename = "primitive"; } else if (type.IsArray) { typename = "array"; } else if (type.IsClass) { typename = "class"; } else { throw new Exception("Unbekannter Typ \"" + type.FullName + "\" für Variable \"" + name + "\"."); } System.Xml.XmlElement xmlElement = xdoc.CreateElement(string.Empty, typename, string.Empty); xmlElement.SetAttribute(strAttributeName, name); if (o != null) { xmlElement.SetAttribute(strAttributeDataType, o.GetType().FullName); if (type.IsPrimitive || type.Equals(typeof(string))) { string str; if (type == typeof(float) || type == typeof(double) || type == typeof(decimal)) { str = Raw.ToString(o); } else { str = o.ToString(); } xmlElement.InnerText = str; } else if (type.IsArray) { int length = ((Array)o).Length; for(int i=0;i /// Erstellt ein Objekt samt Unterobjekten aus einer XML-Datei /// /// Das erstellte Objekt. /// Name der XML-Datei. /// Wenn ungleich null, muss der Objekttyp mit diesem übereinstimmen. public static Object FromXMLFile(string xmlName, Type refType = null) { System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(xmlName); if (xDoc.FirstChild != null && xDoc.FirstChild.NodeType == System.Xml.XmlNodeType.XmlDeclaration) { xDoc.RemoveChild(xDoc.FirstChild); } if (xDoc.ChildNodes.Count != 1) { throw new Exception("Es muss genau ein oberstes XML-Element geben."); } Object ret = ObjectFromXML(null, xDoc.FirstChild); if (refType != null && !ret.GetType().Equals(refType)) { throw new Exception("Ungültiger Objekttyp für Basisobjekt."); } return ret; } } }