1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Xml serialize problem when generating XML output

Discussion in 'C#' started by johnnyVB, Nov 26, 2008.

  1. johnnyVB

    johnnyVB New Member

    Nov 26, 2008
    Likes Received:
    Trophy Points:
    The following is the post I sent out looking for help on the XML Serialization problem:

    I'm having problem with the XML Serialization in how it orders the generated XML with derived classes. This code is slightly modified from one of the examples available on MSDN, in that it runs in a windows form. This example returns a file with this XML:

    The code as modified is:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using System.Net;
    using System.Collections;
    using System.IO;
    namespace TestSerialization
    	public class Book
    		public string ISBN;
    	public class ExpandedBook : Book
    		public bool NewEdition;
    	public class Orders
    		public Book[] Books;
    	public partial class Form1 : Form
    		public Form1()
    		private void Form1_Load(object sender, EventArgs e)
    		public void SerializeObject(string filename)
    			// Each overridden field, property, or type requires 
    			// an XmlAttributes instance.
    			XmlAttributes attrs = new XmlAttributes();
    			// Creates an XmlElementAttribute instance to override the 
    			// field that returns Book objects. The overridden field
    			// returns Expanded objects instead.
    			XmlElementAttribute attr = new XmlElementAttribute();
    			attr.ElementName = "NewBook";
    			attr.Type = typeof(ExpandedBook);
    			// Adds the element to the collection of elements.
    			// Creates the XmlAttributeOverrides instance.
    			XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
    			// Adds the type of the class that contains the overridden 
    			// member, as well as the XmlAttributes instance to override it 
    			// with, to the XmlAttributeOverrides.
    			attrOverrides.Add(typeof(Orders), "Books", attrs);
    			// Creates the XmlSerializer using the XmlAttributeOverrides.
    			XmlSerializer s = new XmlSerializer(typeof(Orders), attrOverrides);
    			// Writing the file requires a TextWriter instance.
    			TextWriter writer = new StreamWriter(filename);
    			// Creates the object to be serialized.
    			Orders myOrders = new Orders();
    			// Creates an object of the derived type.
    			ExpandedBook b = new ExpandedBook();
    			b.NewEdition = true;
    			b.ISBN = "123456789";
    			myOrders.Books = new ExpandedBook[] { b };
    			// Serializes the object.
    			s.Serialize(writer, myOrders);
    		private void button1_Click(object sender, EventArgs e)
    			textBox1.Text = "Done";
    Any help that anyone can give would be appreciated.
    Last edited by a moderator: Nov 27, 2008

Share This Page