HELP! Why is this XMLSerializer Code Wacked?

S

Schorschi

The following code works fine until I check the .CanDeserialize flag

The exact same code in C# works, but in VB .NET? AUGH! HELP!

----------------------------------

Public Function Create(ByVal theOwner As Object, ByVal theContext As
Object, ByVal theNode As System.Xml.XmlNode) As Object Implements
System.Configuration.IConfigurationSectionHandler.Create

Dim theNavigator As XPathNavigator, _
theDescriptor As String, _
theType As Type, _
theSerializer As XmlSerializer, _
theReader As XmlNodeReader

'

theNavigator = theNode.CreateNavigator
theDescriptor = CType(theNavigator.Evaluate("string(@type)"),
String)
theType = theDescriptor.GetType

theSerializer = New XmlSerializer(theType)
theReader = New XmlNodeReader(theNode)

If (theSerializer.CanDeserialize(theReader)) Then

Create = theSerializer.Deserialize(theReader)

End If

Return (Create)

End Function

----------------------------------
Here is the XML Doc...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ProgramInfo"
type="ConfigSectionHandler.ConfigSectionHandler, ConfigSectionHandler"
/>
</configSections>
<ProgramInfo type="ConfigSectionObjects.ProgramInfo,
ConfigSectionObjects">
<Name>ConfigSectionDemo</Name>
<Language>C#</Language>
<Level>Intermediate</Level>
</ProgramInfo>
</configuration>

----------------------------------
C# Code...

public object Create(object parent, object configContext,
System.Xml.XmlNode section)
{
XPathNavigator xNav=section.CreateNavigator();
string typeOfObject=(string) xNav.Evaluate("string(@type)");
Type t=Type.GetType(typeOfObject);
XmlSerializer ser=new XmlSerializer(t);
XmlNodeReader xNodeReader=new XmlNodeReader(section);
return ser.Deserialize(xNodeReader);
}
 
C

Cor Ligthert

I don't know why it is wacked, who made it?

:)

We had just a long discussion in this newsgroup why something in C# works
and not in VBNet the end conlclussion from both the C# guy and the VBNet
guys was that there was a little different in implementation. In this case
the all found the VBNet way was better.

So maybe it is much easier to tell what you want to archieve using VBNet,
transporting 1 by 1 to another language from code has never been the best
solution.

Just my thought,

Cor
 
D

David

The following code works fine until I check the .CanDeserialize flag

The exact same code in C# works, but in VB .NET? AUGH! HELP!

As you suspect, it's a translation problem...

In VB.Net
theType = theDescriptor.GetType

is not the same as C#.
Type t=Type.GetType(typeOfObject);

In fact, both languages work identically here, what you want is...

theType = Type.GetType(theDescriptor)
 
S

Schorschi

I am trying to load the custom config section name in the .config
file. Using deserialize method seemed effective, since I can later
serialize the section parameters later if needed. I found the C#
example, but for the client I have, the source must be in VB .NET...
weird since C# is so interchange-able? But given the client, and how
they are still learning VB.NET for their own internal use, I guess it
makes sense. They are having real trouble going form VB 6 to VB.NET
(people pulling their hair out).

What I can NOT understand is why this code does not work. It looks
fine, but chokes. Thought I would ask the gurus what the heck is
wrong with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top