?
Solved

IXmlSerializable Headache...

Posted on 2008-10-21
3
Medium Priority
?
1,140 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 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