}//end main public static void SerializeExpandableObject(ExpandableObject obj) { var serializer = new DataContractSerializer(typeof(ExpandableObject)); string xmlString; using (var sw = new StringWriter()) { using (var writer = new System.Xml.XmlTextWriter(sw)) { writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable serializer.WriteObject(writer, obj); writer.Flush(); xmlString = sw.ToString(); } } Console.WriteLine(xmlString); } //end method
public int Compare(ExpandableObject x, ExpandableObject y) { return(((T)x[m_attributeName]).CompareTo((T)y[m_attributeName])); }
static void Main(string[] args) { var obj1 = new ExpandableObject(); obj1["num"] = 3; obj1["name"] = "Cameron Block"; obj1["phone_num"] = "402-734-0281"; obj1["email"] = "*****@*****.**"; var obj2 = new ExpandableObject(); obj2["num"] = 2; obj2["name"] = "Glenn Beck"; obj2["phone_num"] = "402-555-5555"; var obj3 = new ExpandableObject(); obj3["num"] = 1; obj3["name"] = "Bill Oriley"; obj3["phone_num"] = "402-555-5555"; var objects = new List <ExpandableObject>(new ExpandableObject[] { obj1, obj2, obj3 }); // sorting: Console.WriteLine("Sorting:"); Comparer <int> c = new Comparer <int>("num"); objects.Sort(c); foreach (var obj in objects) { Console.WriteLine(obj.ToString() + "\n"); }//end for // filtering: Console.WriteLine("Filtering:"); var filtered = from obj in objects where (int)obj["num"] >= 2 select obj; foreach (var obj in filtered) { Console.WriteLine(obj.ToString() + "\n"); } Console.WriteLine("XML Serializing to file: "); string path = @"..\NSA_DB.xml"; XmlSerializer x = new XmlSerializer(obj1.GetType()); StreamWriter writer = new StreamWriter(path); x.Serialize(writer, obj1); writer.Close(); //make sure the serialization works obj1 = null; Console.WriteLine(); //the following code not working right, error in serialization file. x = new XmlSerializer(typeof(ExpandableObject)); StreamReader reader = new StreamReader(path); obj1 = (ExpandableObject)x.Deserialize(reader); Console.WriteLine("Deserialized output: \n"); Console.WriteLine(obj1.ToString()); Console.ReadLine(); }//end main