Solved

IXmlSerializable Headache...

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now