Click here to Skip to main content
15,885,365 members
Home / Discussions / C#
   

C#

 
GeneralRe: Save objects with derived classes to disc Pin
larsp77724-Apr-12 6:02
larsp77724-Apr-12 6:02 
GeneralRe: Save objects with derived classes to disc Pin
Eddy Vluggen24-Apr-12 6:15
professionalEddy Vluggen24-Apr-12 6:15 
GeneralRe: Save objects with derived classes to disc Pin
larsp77724-Apr-12 6:21
larsp77724-Apr-12 6:21 
GeneralRe: Save objects with derived classes to disc Pin
Dave Kreskowiak24-Apr-12 8:51
mveDave Kreskowiak24-Apr-12 8:51 
GeneralRe: Save objects with derived classes to disc Pin
larsp77724-Apr-12 10:31
larsp77724-Apr-12 10:31 
GeneralRe: Save objects with derived classes to disc Pin
Dave Kreskowiak24-Apr-12 15:47
mveDave Kreskowiak24-Apr-12 15:47 
GeneralRe: Save objects with derived classes to disc Pin
larsp77726-Apr-12 7:43
larsp77726-Apr-12 7:43 
AnswerRe: Save objects with derived classes to disc Pin
Alan N24-Apr-12 12:26
Alan N24-Apr-12 12:26 
There's a special constructor for the Xml serialiser to handle non-homogeneous lists

public XmlSerializer (Type type, Type[] extraTypes)

Basically this tells the serialiser about the main list type and any additional types it may encounter within the list.

Code dump to show what I mean
First generic serialiser/deserialiser methods
C#
public static String SerialiseToString<T>(T data, Type[] additionalTypes) {
  XmlSerializer serializer = new XmlSerializer(typeof(T), additionalTypes);
  using (StringWriter writer = new StringWriter()) {
    serializer.Serialize(writer, data);
    return writer.ToString();
  }
}

public static T DeserialiseFromString<T>(String xmlstr, Type[] additionalTypes) {
  using (StringReader reader = new StringReader(xmlstr)) {
    XmlSerializer serializer = new XmlSerializer(typeof(T), additionalTypes);
    T temp = (T)serializer.Deserialize(reader);
    return temp;
  }
}


Serialise a mixed list of Book and EBook as a test
C#
List<Book> lst = new List<Book>();
Book one = new Book();

one.Title = "Swallows and Amazons";
one.Author = "Arthur Ransome";
lst.Add(one);

EBook two = new EBook();
two.Title = "War Horse";
two.Author = "Michael Morpurgo";
two.Platform = "Kindle";
lst.Add(two);
String xmlData = SerialiseToString<List<Book>>(lst, new Type[]{typeof(EBook)});

Gives XML like this (I'm sure you've guessed the structure of the Book and derived EBook classes).
XML
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfBook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Book>
    <Title>Swallows and Amazons</Title>
    <Author>Arthur Ransome</Author>
  </Book>
  <Book xsi:type="EBook">
    <Title>War Horse</Title>
    <Author>Michael Morpurgo</Author>
    <Platform>Kindle</Platform>
  </Book>
</ArrayOfBook>

which can be deserialised correctly with
List<Book> lst = DeserialiseFromString<List<Book>>(xmlData, new Type[]{typeof(EBook)});


Alan.
Questionc# windows authentication offline Pin
k_finamore24-Apr-12 4:59
k_finamore24-Apr-12 4:59 
AnswerRe: c# windows authentication offline Pin
Pete O'Hanlon24-Apr-12 5:09
mvePete O'Hanlon24-Apr-12 5:09 
GeneralRe: c# windows authentication offline Pin
k_finamore24-Apr-12 5:21
k_finamore24-Apr-12 5:21 
AnswerRe: c# windows authentication offline Pin
Eddy Vluggen24-Apr-12 5:41
professionalEddy Vluggen24-Apr-12 5:41 
GeneralRe: c# windows authentication offline Pin
k_finamore24-Apr-12 5:50
k_finamore24-Apr-12 5:50 
GeneralRe: c# windows authentication offline Pin
Pete O'Hanlon24-Apr-12 5:44
mvePete O'Hanlon24-Apr-12 5:44 
GeneralRe: c# windows authentication offline Pin
k_finamore24-Apr-12 5:49
k_finamore24-Apr-12 5:49 
GeneralRe: c# windows authentication offline Pin
Pete O'Hanlon24-Apr-12 5:53
mvePete O'Hanlon24-Apr-12 5:53 
Questionsending a key press to certmgr.msc Pin
MitchG92_2424-Apr-12 4:44
MitchG92_2424-Apr-12 4:44 
AnswerRe: sending a key press to certmgr.msc Pin
Eddy Vluggen24-Apr-12 4:59
professionalEddy Vluggen24-Apr-12 4:59 
GeneralRe: sending a key press to certmgr.msc Pin
MitchG92_2424-Apr-12 5:02
MitchG92_2424-Apr-12 5:02 
AnswerRe: sending a key press to certmgr.msc Pin
Eddy Vluggen24-Apr-12 5:15
professionalEddy Vluggen24-Apr-12 5:15 
QuestionReference: Object reference not set to an instance of an object. Pin
MemberDotNetting24-Apr-12 4:22
MemberDotNetting24-Apr-12 4:22 
QuestionRe: Reference: Object reference not set to an instance of an object. Pin
Eddy Vluggen24-Apr-12 4:58
professionalEddy Vluggen24-Apr-12 4:58 
AnswerRe: Reference: Object reference not set to an instance of an object. Pin
MemberDotNetting24-Apr-12 5:04
MemberDotNetting24-Apr-12 5:04 
AnswerRe: Reference: Object reference not set to an instance of an object. Pin
Eddy Vluggen24-Apr-12 5:18
professionalEddy Vluggen24-Apr-12 5:18 
GeneralRe: Reference: Object reference not set to an instance of an object. Pin
MemberDotNetting24-Apr-12 5:32
MemberDotNetting24-Apr-12 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.