XML Serialization
-
You may recall I asked about binary serialization earlier. I did it but (after a suggestion from someones' reply) I wanted to try something I never tried before so I want to get my feet wet in XML. As you may recall, I have 2 classes of lists derived from collections (class ArrayList to be specific) with objects in the list (also derived from System.Object). I can serialize both of them into one file but in the file I get:
<MyArray1 count="6">
...
</MyArray1><?xml version="1.0" encoding="utf-8"?>
<MyArray2 count="2">
...
</MyArray2>That "?xml" tag, which as I understand is the document start tag that states it's XML, what version of XML it is and what encoding it has, should only be at the top and not in the middle. Here's the core of the XML code:
StreamWriter writer = new StreamWriter(dlg.FileName); XmlSerializer ser1 = new XmlSerializer(typeof(Array1)); ser1.Serialize(writer, array1); XmlSerializer ser2 = new XmlSerializer(typeof(Array2)); ser.Serialize(writer, array2); writer.Close();
I figure it's doing that because the second XML serializer figures it's the only XML serializer so it writes that document start tag which I want to get out of there. I found an overloaded constructor for the XmlSerializer which is (Type, Type[]). I figure that's what I'm looking for because the second argument takes "extra types". I tried creating a Type array with a single element (the typeof(Array2)) and the first argument keeps typeof(Array1) but in runtime, I get an exception that says "Specified cast is not valid" at Write1_Array1. Some web pages say it's because it's trying to cast my derived class as the base class and it doesn't like that but I don't know if they are having the same issue. I don't quite know how to fix this, can anyone help me? -
You may recall I asked about binary serialization earlier. I did it but (after a suggestion from someones' reply) I wanted to try something I never tried before so I want to get my feet wet in XML. As you may recall, I have 2 classes of lists derived from collections (class ArrayList to be specific) with objects in the list (also derived from System.Object). I can serialize both of them into one file but in the file I get:
<MyArray1 count="6">
...
</MyArray1><?xml version="1.0" encoding="utf-8"?>
<MyArray2 count="2">
...
</MyArray2>That "?xml" tag, which as I understand is the document start tag that states it's XML, what version of XML it is and what encoding it has, should only be at the top and not in the middle. Here's the core of the XML code:
StreamWriter writer = new StreamWriter(dlg.FileName); XmlSerializer ser1 = new XmlSerializer(typeof(Array1)); ser1.Serialize(writer, array1); XmlSerializer ser2 = new XmlSerializer(typeof(Array2)); ser.Serialize(writer, array2); writer.Close();
I figure it's doing that because the second XML serializer figures it's the only XML serializer so it writes that document start tag which I want to get out of there. I found an overloaded constructor for the XmlSerializer which is (Type, Type[]). I figure that's what I'm looking for because the second argument takes "extra types". I tried creating a Type array with a single element (the typeof(Array2)) and the first argument keeps typeof(Array1) but in runtime, I get an exception that says "Specified cast is not valid" at Write1_Array1. Some web pages say it's because it's trying to cast my derived class as the base class and it doesn't like that but I don't know if they are having the same issue. I don't quite know how to fix this, can anyone help me?You can only have 1 root object in XML, so you have to wrap all your instances in a single class (could be a dummy class).
-
You can only have 1 root object in XML, so you have to wrap all your instances in a single class (could be a dummy class).
Okay, I understand. However, could I create an instance of XmlRootAttribute, and potentially set some namespaces also instead of using a dummy class?