By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

Using XmlSerializer to deserialize derived types

P: n/a
I have a schema file datamodel.xsd, element "properties" is declared as a
type of "baseProperty". The schema file also defines "derivedProperty" is a
derived type of "baseProperty".

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="uri:myschema" targetNamespace="uri:myschema"
elementFormDefault="qualified" version="1.0">

<xs:complexType name="derivedProperty">
<xs:complexContent>
<xs:extension base="cm:baseProperty">
<xs:attribute name="externalID" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:derivedProperty>

<xs:element name="properties" type="myns:baseProperty"/>
...
</xs:schema>

WSDL.exe generated C# proxy code MyWebService.cs as the following.

/// <remarks/>
public class MyBase {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Names pace="uri:myschema")]
public baseProperty properties;
....
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="uri:myschema")]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(derivedProperty))]
public class baseProperty {
....
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="uri:myschema")]
public class derivedProperty : baseProperty {

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string externalID;
}
I can use MyWebService.cs to deserialize the following Xml successfully.

<ItemReply xmlns="uri:publicschema">
<Form1 fname="John" lname="Tee"
myns:ID="xxxxxx"
xmlns:myns="uri:myschema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
<myns:properties type="doc">
<myns:lastchangeuserid value="user1" />
<myns:lastchangetime value="2006-07-27T06:56:35.574" />
</myns:properties>
<MyBase
myns:ID="xxxxxxx"
myns:serialNumber="1">
<myns:properties type="item" xsi:type="myns:derivedProperty">
<myns:lastchangeuserid value="user1" />
<myns:lastchangetime value="2006-07-26T01:56:35.000" />
</myns:properties>
</MyBase>
</Form1>
</ItemReply>

While I wrote another piece of code XmlSerializer

XmlNodeReader nodeReader = new XmlNodeReader(node);
Form1 theform;
XmlSerializer serializer = new XmlSerializer(typeof(Form1),
"uri:publicschema");
theForm = ((Form1)(serializer.Deserialize(nodeReader)));

It didn't work. I got an error like
{"The specified type was not recognized: name='derivedProperty',
namespace='uri:myschema', at <properties xmlns='uri:myschema'>."}

It seems to me that MyWebService.cs can successfully deserialize ItemReply
message. However XmlSerializer can not find the inheritance relationship
between baseProperty and derivedProperty so that it doesn't know what type of
myns:properties is. I think maybe the way I'm using XmlSerializer is not
correct.
Can anyone tell me how I can write using XmlSerializer to deserializer the
Xml instance showed above correctly?

--
Yewen
Aug 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Aren't anyone here to answer my question?
By the way, I'm using Visual Studio ..Net 2003. .NET Framework SDK v1.1,
..NET Web Service Enhancements SP1 and Hotfix 892202.

Thank you very much.

Yewen,
"Yewen Tang" wrote:
I have a schema file datamodel.xsd, element "properties" is declared as a
type of "baseProperty". The schema file also defines "derivedProperty" is a
derived type of "baseProperty".

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="uri:myschema" targetNamespace="uri:myschema"
elementFormDefault="qualified" version="1.0">

<xs:complexType name="derivedProperty">
<xs:complexContent>
<xs:extension base="cm:baseProperty">
<xs:attribute name="externalID" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:derivedProperty>

<xs:element name="properties" type="myns:baseProperty"/>
...
</xs:schema>

WSDL.exe generated C# proxy code MyWebService.cs as the following.

/// <remarks/>
public class MyBase {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Names pace="uri:myschema")]
public baseProperty properties;
....
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="uri:myschema")]
[System.Xml.Serialization.XmlIncludeAttribute(typeo f(derivedProperty))]
public class baseProperty {
....
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="uri:myschema")]
public class derivedProperty : baseProperty {

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string externalID;
}
I can use MyWebService.cs to deserialize the following Xml successfully.

<ItemReply xmlns="uri:publicschema">
<Form1 fname="John" lname="Tee"
myns:ID="xxxxxx"
xmlns:myns="uri:myschema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
<myns:properties type="doc">
<myns:lastchangeuserid value="user1" />
<myns:lastchangetime value="2006-07-27T06:56:35.574" />
</myns:properties>
<MyBase
myns:ID="xxxxxxx"
myns:serialNumber="1">
<myns:properties type="item" xsi:type="myns:derivedProperty">
<myns:lastchangeuserid value="user1" />
<myns:lastchangetime value="2006-07-26T01:56:35.000" />
</myns:properties>
</MyBase>
</Form1>
</ItemReply>

While I wrote another piece of code XmlSerializer

XmlNodeReader nodeReader = new XmlNodeReader(node);
Form1 theform;
XmlSerializer serializer = new XmlSerializer(typeof(Form1),
"uri:publicschema");
theForm = ((Form1)(serializer.Deserialize(nodeReader)));

It didn't work. I got an error like
{"The specified type was not recognized: name='derivedProperty',
namespace='uri:myschema', at <properties xmlns='uri:myschema'>."}

It seems to me that MyWebService.cs can successfully deserialize ItemReply
message. However XmlSerializer can not find the inheritance relationship
between baseProperty and derivedProperty so that it doesn't know what type of
myns:properties is. I think maybe the way I'm using XmlSerializer is not
correct.
Can anyone tell me how I can write using XmlSerializer to deserializer the
Xml instance showed above correctly?

--
Yewen
Aug 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.