Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Xml serialize problem when generating XML output (http://www.go4expert.com/forums/xml-serialize-generating-xml-output-t15264/)

johnnyVB 26Nov2008 23:12

Xml serialize problem when generating XML output
 
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:


Code:

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()
                {
                        InitializeComponent();
                }
                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.
                        attrs.XmlElements.Add(attr);
                        // 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);
                        writer.Close();
                }
                private void button1_Click(object sender, EventArgs e)
                {
                        SerializeObject(textBox1.Text);
                        textBox1.Text = "Done";
                }
        }
}

Any help that anyone can give would be appreciated.


All times are GMT +5.5. The time now is 16:04.