Using XmlSerializer to deserialize derived types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yewen Tang

    Using XmlSerializer to deserialize derived types

    I have a schema file datamodel.xsd, element "properties " is declared as a
    type of "baseProper ty". The schema file also defines "derivedPropert y" is a
    derived type of "baseProper ty".

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

    <xs:complexTy pe name="derivedPr operty">
    <xs:complexCont ent>
    <xs:extension base="cm:basePr operty">
    <xs:attribute name="externalI D" type="xs:string "/>
    </xs:extension>
    </xs:complexConte nt>
    </xs:derivedPrope rty>

    <xs:element name="propertie s" type="myns:base Property"/>
    ...
    </xs:schema>

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

    /// <remarks/>
    public class MyBase {

    /// <remarks/>
    [System.Xml.Seri alization.XmlEl ementAttribute( Namespace="uri: myschema")]
    public baseProperty properties;
    ....
    }

    /// <remarks/>
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="uri:mys chema")]
    [System.Xml.Seri alization.XmlIn cludeAttribute( typeof(derivedP roperty))]
    public class baseProperty {
    ....
    }

    /// <remarks/>
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="uri:mys chema")]
    public class derivedProperty : baseProperty {

    /// <remarks/>
    [System.Xml.Seri alization.XmlAt tributeAttribut e()]
    public string externalID;
    }


    I can use MyWebService.cs to deserialize the following Xml successfully.

    <ItemReply xmlns="uri:publ icschema">
    <Form1 fname="John" lname="Tee"
    myns:ID="xxxxxx "
    xmlns:myns="uri :myschema"
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <myns:propertie s type="doc">
    <myns:lastchang euserid value="user1" />
    <myns:lastchang etime value="2006-07-27T06:56:35.574 " />
    </myns:properties >
    <MyBase
    myns:ID="xxxxxx x"
    myns:serialNumb er="1">
    <myns:propertie s type="item" xsi:type="myns: derivedProperty ">
    <myns:lastchang euserid value="user1" />
    <myns:lastchang etime value="2006-07-26T01:56:35.000 " />
    </myns:properties >
    </MyBase>
    </Form1>
    </ItemReply>

    While I wrote another piece of code XmlSerializer

    XmlNodeReader nodeReader = new XmlNodeReader(n ode);
    Form1 theform;
    XmlSerializer serializer = new XmlSerializer(t ypeof(Form1),
    "uri:publicsche ma");
    theForm = ((Form1)(serial izer.Deserializ e(nodeReader))) ;

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

    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
  • yewen

    #2
    RE: Using XmlSerializer to deserialize derived types

    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 "baseProper ty". The schema file also defines "derivedPropert y" is a
    derived type of "baseProper ty".
    >
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:myns="uri :myschema" targetNamespace ="uri:mysche ma"
    elementFormDefa ult="qualified" version="1.0">
    >
    <xs:complexTy pe name="derivedPr operty">
    <xs:complexCont ent>
    <xs:extension base="cm:basePr operty">
    <xs:attribute name="externalI D" type="xs:string "/>
    </xs:extension>
    </xs:complexConte nt>
    </xs:derivedPrope rty>
    >
    <xs:element name="propertie s" type="myns:base Property"/>
    ...
    </xs:schema>
    >
    WSDL.exe generated C# proxy code MyWebService.cs as the following.
    >
    /// <remarks/>
    public class MyBase {
    >
    /// <remarks/>
    [System.Xml.Seri alization.XmlEl ementAttribute( Namespace="uri: myschema")]
    public baseProperty properties;
    ....
    }
    >
    /// <remarks/>
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="uri:mys chema")]
    [System.Xml.Seri alization.XmlIn cludeAttribute( typeof(derivedP roperty))]
    public class baseProperty {
    ....
    }
    >
    /// <remarks/>
    [System.Xml.Seri alization.XmlTy peAttribute(Nam espace="uri:mys chema")]
    public class derivedProperty : baseProperty {
    >
    /// <remarks/>
    [System.Xml.Seri alization.XmlAt tributeAttribut e()]
    public string externalID;
    }
    >
    >
    I can use MyWebService.cs to deserialize the following Xml successfully.
    >
    <ItemReply xmlns="uri:publ icschema">
    <Form1 fname="John" lname="Tee"
    myns:ID="xxxxxx "
    xmlns:myns="uri :myschema"
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <myns:propertie s type="doc">
    <myns:lastchang euserid value="user1" />
    <myns:lastchang etime value="2006-07-27T06:56:35.574 " />
    </myns:properties >
    <MyBase
    myns:ID="xxxxxx x"
    myns:serialNumb er="1">
    <myns:propertie s type="item" xsi:type="myns: derivedProperty ">
    <myns:lastchang euserid value="user1" />
    <myns:lastchang etime value="2006-07-26T01:56:35.000 " />
    </myns:properties >
    </MyBase>
    </Form1>
    </ItemReply>
    >
    While I wrote another piece of code XmlSerializer
    >
    XmlNodeReader nodeReader = new XmlNodeReader(n ode);
    Form1 theform;
    XmlSerializer serializer = new XmlSerializer(t ypeof(Form1),
    "uri:publicsche ma");
    theForm = ((Form1)(serial izer.Deserializ e(nodeReader))) ;
    >
    It didn't work. I got an error like
    {"The specified type was not recognized: name='derivedPr operty',
    namespace='uri: myschema', at <properties xmlns='uri:mysc hema'>."}
    >
    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

    Comment

    Working...