Solved

IXmlSerializable Headache...

Posted on 2008-10-21
3
1,128 Views
Last Modified: 2013-12-17
Can anyone explain to me my error? The object refuses to deserialize correctly. I have a manager object to control "Banks", and the "BankManager" needs to serialize each bank. Here's the code in the manager:

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("Banks");
            foreach (BaseBank b in banks.Values.ToList())
            {
                writer.WriteStartElement("Bank");
                writer.WriteAttributeString("Name", b.Name);
                writer.WriteAttributeString("Type", b.GetType().AssemblyQualifiedName);
                XmlSerializer xx = new XmlSerializer(b.GetType());
                xx.Serialize(writer, b);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement(); //Start reading the BankManager element
                                       //Part of the IXmlSerializer contract
            reader.ReadStartElement("Banks"); //Start reading the Banks element
            while (reader.IsStartElement()) //Loop through each starting element
            {
                reader.ReadStartElement(); //Open the bank element
                string name = reader.GetAttribute("Name"); //Read out the name
                string type = reader.GetAttribute("Type"); //and AQN (assy. qualified name)

                XmlSerializer xx = new XmlSerializer(Type.GetType(type));
                BaseBank b = (BaseBank)xx.Deserialize(reader); //Deserialize the information from
                                                               //our xml reader for this object
                b.Name = name;
                banks.Add(b.Name, b);
                reader.ReadEndElement(); //Close the bank element
            }
            reader.ReadEndElement(); //Close out the Banks element
            reader.ReadEndElement(); //Stop reading the BankManager element
                                     //Part of the IXmlSerializer contract
        }

And here's serialization for my FileInputBank:

public override void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString("Source", source.FilePath);
            writer.WriteElementString("Repeat", _repeat.ToString());
        }

        public override void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement();
            source = new FileInput(reader.ReadElementContentAsString("Source", ""));
            _repeat = bool.Parse(reader.ReadElementContentAsString("Repeat", ""));
            reader.ReadEndElement();
        }

The XML written to a file is below (I see no errors, so it must be how I'm reading the XML)
<?xml version="1.0"?>
<BankManager>
  <Banks>
    <Bank Name="ConstantBank1" Type="PluggedIn.Data.Banks.FileInputBank, PluggedIn.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <FileInputBank>
        <Repeat>True</Repeat>
        <Source>C:\sample.txt</Source>
      </FileInputBank>
    </Bank>
  </Banks>
</BankManager>

Open in new window

0
Comment
Question by:tasky
3 Comments
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 22782590
please post the following:

compilable cod that i can easy copy and paste in my VS, compile and run it and see the error you are facing...
0
 
LVL 3

Accepted Solution

by:
tasky earned 0 total points
ID: 22975619
I've solved the problem myself.  Just as a note to anyone coming across similar issues, before you call .ReadStartElement, check if the element is empty; if so, do not call .ReadEndElement as it throws off everything
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question