473,545 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.F ormatters.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("tes t.xml",
FileMode.OpenOr Create, FileAccess.Writ e, FileShare.Write ))
{
root.Save(strm, sf);
strm.Close();
}

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

Console.WriteLi ne("Success.") ;
}
catch (Exception exc)
{
Console.WriteLi ne("An error occurred:");
Console.WriteLi ne(exc.Message) ;
}
Console.ReadLin e();
}
}

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.I Formatter formatter);
void Load(System.IO. Stream strm,
System.Runtime. Serialization.I Formatter 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.descriptio n = 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(c hild)>=0) return; // don't allow duplicates
INode [] newList = new INode[this.children.L ength+1];
this.children.C opyTo(newList, 0);
newList[this.children.L ength] = child;
child.Parent = this;
this.children = newList;
}

public void Remove(INode child)
{
int index = this.IndexOf(ch ild);
if (index<0) return;
INode [] newList = new INode[this.children.L ength-1];
for (int ii=0 ; ii<index ; ii++)
newList[ii] = this.children[ii];
for (int ii=index ; ii<this.childre n.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.childre n.Length ; ii++)
if (this.children[ii] == child) return ii;
return -1;
}

public void Save(System.IO. Stream strm,
System.Runtime. Serialization.I Formatter formatter)
{
int version = 100;
formatter.Seria lize(strm, version);
formatter.Seria lize(strm, this.name);
formatter.Seria lize(strm, this.descriptio n);
//formatter.Seria lize(strm, this.guid);
formatter.Seria lize(strm, this.children.L ength);
foreach (INode child in this.children)
child.Save(strm , formatter);
}

public void Load(System.IO. Stream strm,
System.Runtime. Serialization.I Formatter formatter)
{
try
{
int version = (int)formatter. Deserialize(str m);
this.name = (string)formatt er.Deserialize( strm);
this.descriptio n = (string)formatt er.Deserialize( strm);
//this.guid = (Guid)formatter .Deserialize(st rm);
int numKids = (int)formatter. Deserialize(str m);
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("MyNo de.Load() exception", exc);
}
}

#endregion

#region INamedObject Members

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

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

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

#endregion
}
}
Nov 16 '05 #1
1 2664
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
4726
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 Xml key. and refers to the Xml key: <a1:C_XOptions id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SU2000Plus/sekretariat_RM
3
4840
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 create the classes. I load the file created in my project but when I try to serialize...
2
6281
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 limitation (i.e. public members). SoapFormatter is better to serialize arbitrary data. I have done some tests and SoapFormatter is faster 5 times than...
2
3744
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 implemented using Inherits System.Collections.Specialized.NameObjectCollectionBase and also has the <Serializable()> flag before the class name...
0
1615
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 just goes to the end of the file. After reading the first object, the fStream.Position is pointed to the end of the file. The collection...
0
1084
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 the user to document a collection of art objects. Each object is stored in an arrayList as an objArt. To simplify finding my problem, I have...
1
7466
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 SoapFormatter. Why does the SoapFormatter not support generics?
5
2282
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 deserialized. I have verified that the XML being returned by the service contains the tags I am expecting, but they don't show up in the resulting...
5
2596
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 ASP.Net 2.0 using VB.Net and had developed an application that ran correctly on my development server but would not run on the shared hosting paltform...
0
7425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.