using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; namespace ConsoleApplication1 { public class Bernard { public static void Main() { // Create an instance of our custom Property Wrapper class. PropertyWrapper wrapper = new PropertyWrapper(); // Add a few properties to it. wrapper.AddProperty("Name", "Bernard Bout"); wrapper.AddProperty("DOB", Convert.ToDateTime("01/01/1970")); wrapper.AddProperty("Phone", "+44 161 777 5555"); wrapper.AddProperty("Email", "bernard@someplace.com"); // Get some values we know exist. string nameValue = wrapper.GetValue("Name").ToString(); DateTime dobValue = Convert.ToDateTime(wrapper.GetValue("DOB")); // Output these values to the console. Console.WriteLine("\"Name\" == " + nameValue); Console.WriteLine("\"DOB\" == " + dobValue.ToString()); // Try for one that doesn't. object badValue = wrapper.GetValue("BadValue"); if (badValue == null) { Console.WriteLine("*** \"BadValue\" doesn't exist ***"); } Console.WriteLine(); Console.WriteLine(); // Get hold of the generic dictionary. Dictionary<string, object> properties = wrapper.GetProperties(); // Create a new XML document. XmlDocument doc = new XmlDocument(); // Add a declaration (if you want to) XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "", ""); // Add the declaration to the XML document (if you want to) doc.AppendChild(dec); // Create a root node called "Properties". XmlNode rootNode = doc.CreateElement("Properties"); // Iterate around the dictionary and create an XML document. foreach (KeyValuePair<string, object> pair in properties) { // Create an Xml node that should be a child of the root node. XmlNode property = doc.CreateElement("Property"); // Create an attribute called "Name". XmlAttribute attrib = doc.CreateAttribute("Name"); // Specify the value of this attribute to be the key value of the dictionary entry. attrib.Value = pair.Key; // Add the new attribute to the attribute collection of the property node. property.Attributes.Append(attrib); // Assign the value of the dictionary entry to the InnerText value of the property node. property.InnerText = pair.Value.ToString(); // Add the property node as a child of the rootNode. rootNode.AppendChild(property); } // We have stopped iterating over the dictionary so, add the rootNode to the XmlDocument. doc.AppendChild(rootNode); // Finally, write the XML document out as a string to the xmlData string variable. string xmlData = doc.InnerXml; } } public class PropertyWrapper { #region " Private Member Fields " // Create a private member field called _runtTimeProperties that is of type // generic dictionary where the "key" is based on a string type and the // value is based on an object type. This means that you can add any // value to the dictionary because everything is derived from object. private Dictionary<string, object> _runtTimeProperties = new Dictionary<string, object>(); #endregion #region " AddProperty(string vfpStylePropertyName, object vfpStylePropertValue) " /// <summary> /// This method is a simple wrapper that allows you to add a property/value /// to the internal dictionary as a key/value pair. /// </summary> /// <param name="vfpStylePropertyName"></param> /// <param name="vfpStylePropertValue"></param> public void AddProperty(string vfpStylePropertyName, object vfpStylePropertValue) { try { _runtTimeProperties.Add(vfpStylePropertyName, vfpStylePropertValue); } catch (Exception e) { // Handle duplicate key added to Dictionary. throw new Exception("Error occured adding property", e); } } #endregion #region " GetProperties() " /// <summary> /// Return the dictionary. /// </summary> /// <returns></returns> public Dictionary<string, object> GetProperties() { return _runtTimeProperties; } #endregion #region " GetValue(string propertyName) " /// <summary> /// Get an individual property value. /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public object GetValue(string propertyName) { object outValue = null; // If the propertyName cannot be found in _runtTimeProperties, TryGetValue() // returns false which on this occasion can be discarded. If the value isn't // found, outValue stays null _runtTimeProperties.TryGetValue(propertyName, out outValue); return outValue; } #endregion } }