public void SaveDefaultInfo(ProductInformation information) { XmlDocument doc = new XmlDocument(); XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); doc.AppendChild(declaration); XmlElement root = doc.CreateElement("ProductInformation"); doc.AppendChild(root); Type type = information.GetType(); FieldInfo[] fields = type.GetFields(); foreach (FieldInfo field in fields) { XmlElement fieldElement = doc.CreateElement("Property"); XmlAttribute attribute = doc.CreateAttribute("name"); attribute.Value = field.Name; fieldElement.Attributes.Append(attribute); XmlAttribute valueAttribute = doc.CreateAttribute("value"); valueAttribute.Value = Convert.ToString(field.GetValue(information)); fieldElement.Attributes.Append(valueAttribute); root.AppendChild(fieldElement); } doc.Save(Globals.productInformationFile); }
public void LoadDefaultInfo(ProductInformation information) { if (File.Exists(Globals.productInformationFile)) { XmlDocument document = new XmlDocument(); document.Load(Globals.productInformationFile); XmlNodeList fieldList = document.GetElementsByTagName("Property"); FieldInfo[] fields = information.GetType().GetFields(); foreach (XmlNode fieldNode in fieldList) { foreach (FieldInfo field in fields) { string name = fieldNode.Attributes["name"].Value; if (name == field.Name) { if (field.FieldType == typeof(bool)) { field.SetValue(information, Convert.ToBoolean(fieldNode.Attributes["value"].Value)); } else if (field.FieldType == typeof(UIType)) { field.SetValue(information, Enum.Parse(typeof(UIType), fieldNode.Attributes["value"].Value)); } else { field.SetValue(information, (object)fieldNode.Attributes["value"].Value); } break; } } } } }