private void WriteOverriddenAttributes(string filename) { // Writing the file requires a TextWriter. TextWriter myStreamWriter = new StreamWriter(filename); // Create an XMLAttributeOverrides class. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); // Create the XmlAttributes class. XmlAttributes attrs = new XmlAttributes(); /* Override the Student class. "Alumni" is the name * of the overriding element in the XML output. */ XmlElementAttribute attr = new XmlElementAttribute("Alumni", typeof(Graduate)); /* Add the XmlElementAttribute to the collection of * elements in the XmlAttributes object. */ attrs.XmlElements.Add(attr); /* Add the XmlAttributes to the XmlAttributeOverrides. * "Students" is the name being overridden. */ attrOverrides.Add(typeof(HighSchool.MyClass), "Students", attrs); // Create the XmlSerializer. XmlSerializer mySerializer = new XmlSerializer (typeof(HighSchool.MyClass), attrOverrides); MyClass myClass = new MyClass(); Graduate g1 = new Graduate(); g1.Name = "Jackie"; g1.ID = 1; g1.University = "Alma Mater"; Graduate g2 = new Graduate(); g2.Name = "Megan"; g2.ID = 2; g2.University = "CM"; Student[] myArray = { g1, g2 }; myClass.Students = myArray; mySerializer.Serialize(myStreamWriter, myClass); myStreamWriter.Close(); }
private void WriteOverriddenAttributes(string filename) { // Writing the file requires a TextWriter. TextWriter myStreamWriter = new StreamWriter(filename); // Create an XMLAttributeOverrides class. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); // Create the XmlAttributes class. XmlAttributes attrs = new XmlAttributes(); /* Override the Student class. "Alumni" is the name * of the overriding element in the XML output. */ XmlElementAttribute attr = new XmlElementAttribute("Alumni", typeof(Graduate)); /* Add the XmlElementAttribute to the collection of * elements in the XmlAttributes object. */ attrs.XmlElements.Add(attr); /* Add the XmlAttributes to the XmlAttributeOverrides. * "Students" is the name being overridden. */ attrOverrides.Add(typeof(HighSchool.MyClass), "Students", attrs); // Create array of extra types. Type [] extraTypes = new Type[2]; extraTypes[0] = typeof(Address); extraTypes[1] = typeof(Phone); // Create an XmlRootAttribute. XmlRootAttribute root = new XmlRootAttribute("Graduates"); /* Create the XmlSerializer with the * XmlAttributeOverrides object. */ XmlSerializer mySerializer = new XmlSerializer (typeof(HighSchool.MyClass), attrOverrides, extraTypes, root, "http://www.microsoft.com"); MyClass myClass = new MyClass(); Graduate g1 = new Graduate(); g1.Name = "Jacki"; g1.ID = 1; g1.University = "Alma"; Graduate g2 = new Graduate(); g2.Name = "Megan"; g2.ID = 2; g2.University = "CM"; Student[] myArray = { g1, g2 }; myClass.Students = myArray; // Create extra information. Address a1 = new Address(); a1.City = "Ionia"; Address a2 = new Address(); a2.City = "Stamford"; Phone p1 = new Phone(); p1.Number = "555-0101"; Phone p2 = new Phone(); p2.Number = "555-0100"; Object[] o1 = new Object[2] { a1, p1 }; Object[] o2 = new Object[2] { a2, p2 }; g1.Info = o1; g2.Info = o2; mySerializer.Serialize(myStreamWriter, myClass); myStreamWriter.Close(); }