Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:24:57+00:00 2026-05-13T06:24:57+00:00

I’m trying to generate code from an XSD that has two complexTypes extending a

  • 0

I’m trying to generate code from an XSD that has two complexTypes extending a common complexType. The inheritors have a common property named “value”, which has different types. Of course I can’t put it on my base type due to XSD rules. My intention is to polymorphically call the property on the base type which would invoke the right method on the subclass. Is this possible?

Here is my XSD

<xs:complexType name="Property">
    <xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="StringProperty">
    <xs:complexContent>
        <xs:extension base="Property">
            <xs:attribute name="value" type="xs:string" use="required"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="BooleanProperty">
    <xs:complexContent>
        <xs:extension base="Property">
            <xs:attribute name="value" type="xs:boolean" use="required"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

This generates the following code:

public partial class Property {

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

public partial class StringProperty : Property {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

public partial class BoolProperty : Property {

    private bool valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

And here’s what I want to do:

            // ConfigProperties is an array of Property objects
            foreach (Property property in config.ConfigProperties)
            {
                // Doesn't compile since Property.value doesn't exist in the base class.
                Console.WriteLine(property.value);
            }

I tried implementing a “value” getter property on the Property object hoping the subclasses’ “value” property definitions would hide it, but that doesn’t do the trick:

public partial class Property
{
    public virtual string value
    {
        get { throw new NotImplementedException(); } // this method is called instead of the Property subclass methods
    }
}

FOLLOW-UP QUESTION EDIT: The answers confirm my fears that this is not possible. Would it still be possible though to do the following in some manner:

            foreach (Property property in config.ConfigProperties)
            {
                somevalue = property.GetValue();
            }

where GetValue() is a method that figures out what return type it should have by virtue of the subclass of Property it actually is using? Pardon me for being extremely lazy. I’m given to believe that it’s a virtue. =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:24:57+00:00Added an answer on May 13, 2026 at 6:24 am

    Your are deserializing into Property objects, therefore the serializer creates objects of the type Property. You will not know at runtime whether you had a BoolProperty or a StringProperty.

    You need to extract the StringProperties from the XML, deserialize them into StringProperty objects, and then the same for BoolProperties.

    You can then combine them into one list of the type Property. The app will then behave like expected as long as you are creating a virtual property on your baseclass which you override in your implementation classes.


    response to author’s edit
    It will not work, as you do not have the correct information at that point. You will have to explicitly deserialize your object as a StringProperty to have .NET recognize your property. You will never be able to detect the propertytype in your function.

    If you have Property objects of the correct type, you can create an extension method to facilitate:

    public string GetValue(this Property property)
    {
        if(property is StringProperty) return ((StringProperty)property).Value;
        else if ...
    }
    

    Yet you need to know the correct type here, which you do not know if you are deserializing your entities as Property objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 288k
  • Answers 288k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer These are called generics. Here's how you use several of… May 13, 2026 at 5:17 pm
  • Editorial Team
    Editorial Team added an answer EDIT: Jimmy has mentioned in the comments that AutoMapper uses… May 13, 2026 at 5:17 pm
  • Editorial Team
    Editorial Team added an answer Tanks for your comments! I found a workaround: //Remove Observer… May 13, 2026 at 5:17 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.