473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Problems with SoapFormatter and Deserialization

I have an application where I have to make a tree of objects. To do
this, I have my own node class. At certain points in the application,
I need to save data. I am having a problem with the SoapFormatter.
The following program gets the exception: "The data at the root level
is invalid. Line 55, position -460."

This code works fine if I use a BinaryFormatter instead of a
SoapFormatter.
Any ideas? Here is a test program:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

namespace SoapTest
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
try
{
// Build up a very simple tree: a root with 4 children
MyNode root = new MyNode("Root Node", "The root of the
tree");
root.Add(new MyNode("Child 1", "Child node #1"));
root.Add(new MyNode("Child 2", "Child node #2"));
root.Add(new MyNode("Child 3", "Child node #3"));
root.Add(new MyNode("Child 4", "Child node #4"));

SoapFormatter sf = new SoapFormatter();
using (Stream strm = new FileStream("test.xml",
FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
root.Save(strm, sf);
strm.Close();
}

using (Stream strm = new FileStream("test.xml",
FileMode.Open, FileAccess.Read, FileShare.Read))
{
root.Load(strm, sf);
strm.Close();
}

Console.WriteLine("Success.");
}
catch (Exception exc)
{
Console.WriteLine("An error occurred:");
Console.WriteLine(exc.Message);
}
Console.ReadLine();
}
}

public interface INamedObject
{
string Name { get; set; }
string Description { get; set; }
Guid Id { get; }
}

public interface INode : INamedObject
{
INode Parent { get; set; }
INode [] Children { get; }
void Add(INode child);
void Remove(INode child);
int IndexOf(INode child);
void Save(System.IO.Stream strm,
System.Runtime.Serialization.IFormatter formatter);
void Load(System.IO.Stream strm,
System.Runtime.Serialization.IFormatter formatter);
}

public class MyNode : INode
{
string name = "unnamed";
string description = "no description";
Guid guid = new Guid();
INode parent = null;
INode [] children = new INode[0];

public MyNode()
{
}

public MyNode(string name, string desc)
{
this.name = name;
this.description = desc;
}

#region INode Members

public INode Parent
{
get
{
return this.parent;
}
set
{
this.parent = value;
}
}

public INode[] Children
{
get
{
return this.children;
}
}

public void Add(INode child)
{
if (child==null) return; // don't allow null children
if (this.IndexOf(child)>=0) return; // don't allow duplicates
INode [] newList = new INode[this.children.Length+1];
this.children.CopyTo(newList, 0);
newList[this.children.Length] = child;
child.Parent = this;
this.children = newList;
}

public void Remove(INode child)
{
int index = this.IndexOf(child);
if (index<0) return;
INode [] newList = new INode[this.children.Length-1];
for (int ii=0 ; ii<index ; ii++)
newList[ii] = this.children[ii];
for (int ii=index ; ii<this.children.Length-1 ; ii++)
newList[ii] = this.children[ii+1];
this.children = newList;
child.Parent = null;
}

public int IndexOf(INode child)
{
for (int ii=0 ; ii<this.children.Length ; ii++)
if (this.children[ii] == child) return ii;
return -1;
}

public void Save(System.IO.Stream strm,
System.Runtime.Serialization.IFormatter formatter)
{
int version = 100;
formatter.Serialize(strm, version);
formatter.Serialize(strm, this.name);
formatter.Serialize(strm, this.description);
//formatter.Serialize(strm, this.guid);
formatter.Serialize(strm, this.children.Length);
foreach (INode child in this.children)
child.Save(strm, formatter);
}

public void Load(System.IO.Stream strm,
System.Runtime.Serialization.IFormatter formatter)
{
try
{
int version = (int)formatter.Deserialize(strm);
this.name = (string)formatter.Deserialize(strm);
this.description = (string)formatter.Deserialize(strm);
//this.guid = (Guid)formatter.Deserialize(strm);
int numKids = (int)formatter.Deserialize(strm);
this.children = new INode[numKids];
for (int ii=0 ; ii<numKids ; ii++)
{
this.children[ii] = new MyNode();
this.children[ii].Load(strm, formatter);
this.children[ii].Parent = this;
}
}
catch (Exception exc)
{
throw new Exception("MyNode.Load() exception", exc);
}
}

#endregion

#region INamedObject Members

public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}

public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}

public Guid Id
{
get
{
return this.guid;
}
}

#endregion
}
}
Nov 16 '05 #1
1 2648
Jim S wrote:
I have an application where I have to make a tree of objects. To do
this, I have my own node class. At certain points in the application,
I need to save data. I am having a problem with the SoapFormatter.
The following program gets the exception: "The data at the root level
is invalid. Line 55, position -460."

This code works fine if I use a BinaryFormatter instead of a
SoapFormatter.

The SoapFormatter supports only one Deserialize call per instance,
so you can use Serialize only once. I don't exactly know why.
You must redesign your code.

bye
Rob
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wiktor Zychla | last post by:
After signing all my assemblies with strong keys, I've found that the application refuses to deserialize any SOAP serialized data. The message says: Parse error, no assembly associated with the...
3
by: AA | last post by:
Hello I am having problems with a very simple serialization :( I download the schema http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-0 and execute the xsd.exe for...
2
by: Jarda | last post by:
Hi all, i'm looking for some performance comparison some charts. I have read the xmlserializer is better, more flexible and faster (analyzing data types in constructor), but there are some...
2
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is...
0
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and...
0
by: GrandpaB | last post by:
I have an error that I have not been able to find. Any insight you can offer would be most appreciated. I believe that the problem is associated with the SoapFormatter. The application allows...
1
by: Hotlips | last post by:
I'm trying to use an SoapFormatter with a generic List (List<int>) (.NET 2.0) When I use it I get an exception that states that the collections from Generic-namespace cannot be used with the...
5
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being...
5
by: =?Utf-8?B?TWFydHluIEZld3RyZWxs?= | last post by:
Hi there. I posted an earlier issue under the name "That assembly does not allow partially trusted callers" but have now identified what the issue is. As explained before I am working in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.