c# - XSD.exe inheritance properties -
i'm trying create schema file using xsd.exe against class has set of members inherit abstract class. finding although abstract class defined in schema file, it's properties not accessible in derived types, when validating xml against schema.
the exception is:
"the element 'findelement' has invalid kid element 'plugins'. list of possible elements expected: 'webelement'."
to help illustrate, provide of c# code, , parts of schema xsd.exe generates.
code - derived type
[serializable] public class findelement : baseexecutable { private iwebelement _element = null; private timespan _waittimeout; private object _option = null; private webelement _webelement = null; [xmlchoiceidentifier("findelementtype")] [xmlelement("id", typeof(string))] [xmlelement("classname", typeof(string))] [xmlelement("cssselector", typeof(string))] [xmlelement("linktext", typeof(string))] [xmlelement("name", typeof(string))] [xmlelement("partiallinktext", typeof(string))] [xmlelement("tagname", typeof(string))] [xmlelement("xpath", typeof(string))] public object alternative { { if (this._option isdriver) (this._option isdriver).root = base.root; homecoming this._option; } set { if (value isdriver) (value isdriver).root = base.root; _option = value; } } [xmlattribute("usewait")] public bool usewait { get; set; } [xmlattribute("waittimeout"), defaultvalue(0)] public long waittimeoutmilliseconds { { homecoming (long)_waittimeout.totalmilliseconds; } set { _waittimeout = new timespan(value * 10000); } } [xmlelement("webelement")] public webelement webelement { { if (this._webelement isdriver) this._webelement.root = base.root; homecoming this._webelement; } set { if (value isdriver) (value isdriver).root = base.root; this._webelement = value; } } public findelement() { this.webelement = new webelement { }; } } code - base of operations type
[serializable] public abstract class baseexecutable : isdriver, icloneable { private list<format> _formatters = null; private conditional _conditional = null; [xmlattribute("screenshot"), defaultvalue(screenshottype.none)] public virtual screenshottype screenshottype { get; set; } [xmlattribute("screenshotsize"), defaultvalue(screenshotsizetype.tn)] public virtual screenshotsizetype screenshotsizetype { get; set; } [xmlattribute("name")] public virtual string name { get; set; } [xmlattribute("description")] public virtual string description { get; set; } [xmlarray("messages")] [xmlarrayitem("message")] public virtual list<message> messages { get; set; } [xmlattribute("message")] public string message { get; set; } [xmlattribute("msglevel"), defaultvalue(msgleveltype.off)] public msgleveltype msglevel { get; set; } [xmlattribute("continueonfail"), defaultvalue(true)] public virtual bool continueonfail { get; set; } [xmlattribute("key")] public string key { get; set; } [xmlarray("plugins")] [xmlarrayitem("plugin")] public list<plugin> plugins { get; set; } [xmlelement("conditional")] public conditional conditional { { if (this._conditional isdriver) (this._conditional isdriver).root = this.root; homecoming this._conditional; } set { // in case added type later doesn't implement isdriver. if (value isdriver) (value isdriver).root = this.root; _conditional = value; } } [xmlelement("expression")] public string look { get; set; } [xmlarray("formatters")] [xmlarrayitem("formatter")] public list<format> formatters { { foreach (isdriver f in this._formatters ?? new list<format>()) f.root = this.root; homecoming this._formatters; } set { foreach (isdriver f in value) f.root = this.root; this._formatters = value; } } public baseexecutable() { this.continueonfail = false; this.plugins = new list<plugin>(); } } from standpoint of inheritance, "plugins" public, , available in derived types when deserializing. trying create schema enforce xml, see xsd.exe not emitting definition recognizes properties base of operations class.
xsd schema - derived type
<xs:complextype name="findelement"> <xs:complexcontent mixed="false"> <xs:extension base="baseexecutable"> <xs:sequence> <xs:choice minoccurs="1" maxoccurs="1"> <xs:element minoccurs="0" maxoccurs="1" name="cssselector" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="xpath" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="classname" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="linktext" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="id" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="name" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="tagname" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="partiallinktext" type="xs:string" /> </xs:choice> <xs:element minoccurs="0" maxoccurs="1" name="webelement" type="webelement" /> </xs:sequence> <xs:attribute name="usewait" type="xs:boolean" use="required" /> <xs:attribute default="0" name="waittimeout" type="xs:long" /> </xs:extension> </xs:complexcontent> </xs:complextype> xsd schema - base of operations type
<xs:complextype name="baseexecutable"> <xs:sequence> <xs:element minoccurs="0" maxoccurs="1" name="messages" type="arrayofmessage" /> <xs:element minoccurs="0" maxoccurs="1" name="plugins" type="arrayofplugin" /> <xs:element minoccurs="0" maxoccurs="1" name="conditional" type="conditional" /> <xs:element minoccurs="0" maxoccurs="1" name="expression" type="xs:string" /> <xs:element minoccurs="0" maxoccurs="1" name="formatters" type="arrayofformat" /> </xs:sequence> <xs:attribute default="none" name="screenshot" type="screenshottype" /> <xs:attribute default="tn" name="screenshotsize" type="screenshotsizetype" /> <xs:attribute name="name" type="xs:string" /> <xs:attribute name="description" type="xs:string" /> <xs:attribute name="message" type="xs:string" /> <xs:attribute default="off" name="msglevel" type="msgleveltype" /> <xs:attribute default="true" name="continueonfail" type="xs:boolean" /> <xs:attribute name="key" type="xs:string" /> </xs:complextype> test xml file
<sdriver xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" msglevel="info"> <packages> <package packagename="google test"> <drivers> <driver> <actions> <action screenshot="after"> <navigate> <gottourl url="https://www.google.com/" msglevel="info" /> </navigate> </action> <action name="actiontitleelement" screenshot="after"> <findelement usewait="false" msglevel="info"> <xpath>/html/head/title</xpath> <plugins> <plugin name="custom.plugin" /> </plugins> </findelement> </action> </actions> </driver> </drivers> </package> </packages> is there way alter schema, either manually or using xsd.exe recognize properties base of operations type?
c# xml serialization xsd
No comments:
Post a Comment