Alrighty, here's more complete code...
----------------------------------------
'MyCustomClass
Namespace MyOrg.SEED
<Serializable()> Public Class User
<XmlElementAttribute(ElementName:="Agency", Type:=GetType(_AgencyFull))>
Public Agency() As _AgencyFull
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public nID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public StartDate As String
<XmlAttributeAttribute()> Public Status As String
<Serializable()> Public Class _AgencyFull
Inherits _Agency
<XmlElementAttribute(ElementName:="Address",
Type:=GetType(_Address))> Public Address As _Address
End Class
End Class
<Serializable()> Public MustInherit Class _Agency
<XmlAttributeAttribute()> Public ID As String
<XmlAttributeAttribute()> Public Name As String
<XmlAttributeAttribute()> Public Primary As Boolean
End Class
<Serializable()> Public Class _Address
<XmlAttributeAttribute(AttributeName:="line1")> Public Line1 As String
<XmlAttributeAttribute(AttributeName:="line2")> Public Line2 As String
<XmlAttributeAttribute(AttributeName:="city")> Public City As String
<XmlAttributeAttribute(AttributeName:="state")> Public State As String
<XmlAttributeAttribute(AttributeName:="postalcode")> Public ZipCode As
String
<XmlAttributeAttribute(AttributeName:="country")> Public Country As
String
End Class
End Namespace
----------------------------------------
'MyInheritsCustomClass
NameSpace TheirOrg.SEED
<Serializable()> Public Class User
Inherits MyOrg.SEED.User
<NonSerialized()> Public XML As String
End Class
End Namespace
'In the application defining the MyInheritsCustomClass
(TheirOrg.SEED.User), I would have a function that retrieves the
deserializable XML from a Web Service, and deserializes it into the derived
class for return...
Public Function LoadUser(ByVal username As String) As TheirOrg.SEED.User
Dim oUser as New TheirOrg.SEED.User
Dim sXML As String = MyWebServiceProxy.GetUser(username)
Dim mynull As XmlParserContext
Dim rXML As XmlReader = New XmlTextReader(sXML, XmlNodeType.Document,
mynull)
Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))
oUser = xSer.Deserialize(rXML)
oUser.XML = sXML
Return oUser
End Function
The XML document looks like this...
<User ID="23" nID="1 " Name="User X"
StartDate="2004-07-19T10:01:13.300" Status="XA">
<Agency ID="56EXD" Name="Organization X" Primary="1"/>
</User>
Forget that the Agency address is not represented in the XML, it's omission
does not affect the deserialization. So, the error I'm getting is in the
line...
Dim xSer As New XmlSerializer(GetType(TheirOrg.SEED.User))
If I change the 'TheirOrg' to 'MyOrg' in the function, and comment out the
"oUser.XML = sXML", the Myorg.SEED.User class is returned just fine. Adding
in the <XmlRoot> clause in both namespaces does not change the error when I
try to initialize the XmlSerializer.
Hope this sheds some more light on the problem.
Kirby Turner said:
The "error in XML document (1,2)" message is caused because the XML
does not match the schema needed to deserialize into
MyInheritsCustomClass. In other words, the root element found at
(1,2) does not match the expected root element. The XML returned from
your web service follows the schema for the class structure
MyCustomClass. To deserialize into an instance of
MyInheritsCustomClass, you must make sure the XML schema for the
MyInheritsCustomClass matches the schema for MyCustomClass. Jakob's
reply has example of this but there is one bug in the example. He
forgot to inherit from MyCustomClass in MyInheritsCustomClass.
public class MyCustomClass
{
// ...
}
[XmlRoot( "MyCustomClass" )]
public class MyInheritsCustomClass : MyCustomClass
{
// ...
}
Please post your class structures so we can see what the problem is if
this still does not work for you.
-KIRBY
I translated your example into VB, but unfortunately, it did not work. I
still had the same "error reflecting type" message. Interestingly, if I
left in the XmlRoot declaration on the MyCustomClass, and tried to
desreialize it instead of the MyInheritsCustomClass, I get an "error in XML
document (1, 2)" error. I take it out, I can deserialize it.
It is possible if you mark your base class and the inherited class with
the
XmlRoot attribute. I.e. (I hope it is ok that I am giving the example in
C#):
[XmlRoot(Namespace = "
www.domain.com", ElementName = "MyClassName")]
public class MyInheritsCustomClass
{
// ...
}
[XmlRoot(Namespace = "
www.domain.com", ElementName = "MyClassName")]
public class MyCustomClass
{
// ...
}
You can now construct XmlSerializers to serialize and deserialize with the
following code:
// Serialize
XmlSerializer serial = new XmlSerializer(typeof(MyCustomClass));
// Deserialize
XmlSerializer serial = new XmlSerializer(typeof(MyInheritsCustomClass));
Hope, this helps.
Regards, Jakob.
:
I know the subject line is a little confusing, but I always try to use
words
that are searchable for anyone else having a similar problem.
Here's what I've got...
I'm retreiving an XML string from a Web Service that represents a
serialized
custom class [MyCustomClass]. On my client, I have a class that
inherits
from that custom class [MyInheritsCustomClass], and contains just one
new
property. Once I get the XML string from the WebMethod, I want to
Deserialize it into the client's class that inherits from the original
MyCustomClass.
Problems I'm running into...
1) The statement...
Dim ser As New XmlSerializer(GetType(MyInheritsCustomClass))
....gives me an "There was an error reflecting type
'MyInheritsCustomClass'"
exception
If I change this to...
Dim ser As New XmlSerializer(GetType(MyCustomClass))
.... I can create the XmlSerializer fine, but then I can't cast the
deserialized MyCustomClass object I get into a MyInheritsCustomClass
object.
Am I missing something? Or is this just not possible for some reason?