Solved

IXmlSerializable Headache...

Posted on 2008-10-21
3
1,129 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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