Solved

IXmlSerializable Headache...

Posted on 2008-10-21
3
1,131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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