470,615 Members | 2,165 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,615 developers. It's quick & easy.

SOAP XMLTypeMapping Deserialization

I'm trying to serialize/deserialize an object in SOAP-encoded form.
I'm using the code below. Serialization succeeds fine, but when I try
to deserialize I get an exception. Can anyone tell me how I can
deserialize an object which has been serialized this way? Do I need
to do something special with an XmlReader?

(I don't have access to the underlying objects, so SoapFormatter is
not an option for me.)

Thanks,
Ben

public void save(object obj, String filepath) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(obj.Ge tType());
StreamWriter writer = null;
XmlTextWriter xw = null;
try {
writer = new StreamWriter(new FileStream(filepath,
FileMode.Create,
FileAccess.Write,
FileShare.None));
xw = new XmlTextWriter(writer);
XmlSerializer ser = new XmlSerializer(xmlType);
xw.WriteStartDocument();
xw.WriteStartElement("root");
ser.Serialize(xw, obj);
xw.WriteEndElement();
} finally {
if (xw != null) {
xw.Close();
}
if (writer != null) {
writer.Close();
}
}
}

public object load(String filepath, Type type) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(type);
StreamReader reader = null;
try {
XmlSerializer xmlSerializer = new XmlSerializer(xmlType);
reader = new StreamReader(new FileStream(filepath,
FileMode.Open,
FileAccess.Read,
FileShare.Read));
object o = xmlSerializer.Deserialize(reader);
return o;
} finally {
if (reader != null) {
reader.Close();
}
}
}
Nov 11 '05 #1
3 5902
What kind of exception do you get when deserializing?

~Martin Chamberlain

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
"Ben Engber" <be*******@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
I'm trying to serialize/deserialize an object in SOAP-encoded form.
I'm using the code below. Serialization succeeds fine, but when I try
to deserialize I get an exception. Can anyone tell me how I can
deserialize an object which has been serialized this way? Do I need
to do something special with an XmlReader?

(I don't have access to the underlying objects, so SoapFormatter is
not an option for me.)

Thanks,
Ben

public void save(object obj, String filepath) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(obj.Ge tType());
StreamWriter writer = null;
XmlTextWriter xw = null;
try {
writer = new StreamWriter(new FileStream(filepath,
FileMode.Create,
FileAccess.Write,
FileShare.None));
xw = new XmlTextWriter(writer);
XmlSerializer ser = new XmlSerializer(xmlType);
xw.WriteStartDocument();
xw.WriteStartElement("root");
ser.Serialize(xw, obj);
xw.WriteEndElement();
} finally {
if (xw != null) {
xw.Close();
}
if (writer != null) {
writer.Close();
}
}
}

public object load(String filepath, Type type) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(type);
StreamReader reader = null;
try {
XmlSerializer xmlSerializer = new XmlSerializer(xmlType);
reader = new StreamReader(new FileStream(filepath,
FileMode.Open,
FileAccess.Read,
FileShare.Read));
object o = xmlSerializer.Deserialize(reader);
return o;
} finally {
if (reader != null) {
reader.Close();
}
}
}

Nov 11 '05 #2
Ben,

the code below works for me. Is this similar to what you are using:

public static object load(String filepath, Type type)
{
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(type);
XmlTextReader reader = null;
object o = null;
try
{
XmlSerializer xmlSerializer = new XmlSerializer(xmlType);
reader = new XmlTextReader( filepath );
bool keepGoing = true;
while( keepGoing )
{
// fast forward to the root element
keepGoing = reader.Read() && ( "root" != reader.Name );
}
reader.Read(); // get past the root element
o = xmlSerializer.Deserialize(reader);
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return o;
}

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Ben Engber" <be*******@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
A good question. Save() works fine. Here is the exception I get when
I invoke Load():

System.InvalidOperationException: There is an error in XML document
(2, 2). ---> System.InvalidOperationException: <root xmlns=''> was not
expected.

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationReader1.Read6_
AssetSet()
--- End of inner exception stack trace ---

at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader
xmlReader, String encodingStyle)

at System.Xml.Serialization.XmlSerializer.Deserialize (TextReader
textReader)

at NFAQ.DAL.FileDataAccess.load(String filepath, Type type) in
c:\build\nfaaddin-noinstaller\nfaq.dal\filedataaccess.cs:line 67

I figure I need to use an XmlTextReader to consume the outermost
element much like how I write it with the XmlTextWriter. But
everything I try fails with similar exceptions. How exactly do I need
to process the input (or output) so that I can deserialize it?

Thanks,
Ben


"SQL Server Development Team [MSFT]" <sq****@microsoft.com> wrote in

message news:<e1**************@TK2MSFTNGP10.phx.gbl>...
What kind of exception do you get when deserializing?

~Martin Chamberlain

Nov 11 '05 #3
Your code works like a charm. The problem was that I was misusing XmlTextReader.

Thank you for the help!

-Ben
Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bob Rundle | last post: by
3 posts views Thread by parrot toes | last post: by
3 posts views Thread by Jon Paugh | last post: by
3 posts views Thread by JRey | last post: by
4 posts views Thread by Jon Davis | last post: by
6 posts views Thread by J. Dudgeon | last post: by
2 posts views Thread by =?Utf-8?B?RmFicmljaW8gRmVycmVpcmE=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.