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 8576009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:49:36+00:00 2026-06-11T19:49:36+00:00

Can anyone suggest how to create a c# class to reflect the following example

  • 0

Can anyone suggest how to create a c# class to reflect the following example of XML structure please? Basically, the sub-element within the element Value can contain either Integer or String elements.

<Preferences>
   <Preference>
      <Tag>CY3A</Tag>
      <Precedence>XML</Precedence>
      <Value>
         <Length>4</Length>
         <Integer>0</Integer>
      </Value>
   </Preference>    
   <Preference>
      <Tag>CYRV</Tag>
      <Precedence>XML</Precedence>
      <Value>
         <Length>16</Length>
         <String>0000 00 @00000</String>
      </Value>
   </Preference>
</Preferences>

I have tried to define the class as follows but it didn’t result in what I am looking for. It generates the unwanted d element.

public class ProgrammingConfiguration
{
   public int Version { get; set; }
   public preferences Preferences { get; set; }
}

public class preferences
{
   public preference Preference { get; set; }
}

public class preference
{
   public string Tag { get; set; }
   public string Precedence { get; set; }                                     
   public value Value { get; set; }
}

public class value
{
   [XmlElement]
   public int Length;

   [XmlElement(Type = typeof(Integer)),
    XmlElement(Type = typeof(HexBinary))]
   public object datafield;
}

public class Integer
{          
   public string d;            
}  

public class String
{            
   public string d;
}        
  • 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-06-11T19:49:37+00:00Added an answer on June 11, 2026 at 7:49 pm

    I normally create an xsd and then use the xsd.exe tool to create a .cs class file from the xsd.

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.269
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=4.0.30319.1.
    // 
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class Preferences {
    
        private PreferencesPreference[] itemsField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Preference", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public PreferencesPreference[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class PreferencesPreference {
    
        private string tagField;
    
        private string precedenceField;
    
        private PreferencesPreferenceValue[] valueField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Tag {
            get {
                return this.tagField;
            }
            set {
                this.tagField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Precedence {
            get {
                return this.precedenceField;
            }
            set {
                this.precedenceField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Value", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public PreferencesPreferenceValue[] Value {
            get {
                return this.valueField;
            }
            set {
                this.valueField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class PreferencesPreferenceValue {
    
        private string lengthField;
    
        private string stringField;
    
        private string integerField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Length {
            get {
                return this.lengthField;
            }
            set {
                this.lengthField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string String {
            get {
                return this.stringField;
            }
            set {
                this.stringField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string Integer {
            get {
                return this.integerField;
            }
            set {
                this.integerField = value;
            }
        }
    }
    

    xsd.exe is a utility that comes with Visual Studio….

    Have a look at http://quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDFromCls.aspx

    But you can do it from the XML too

    c:\temp>xsd test.xml
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'c:\temp\test.xsd'.
    
    c:\temp>xsd test.xsd /classes
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'c:\temp\test.cs'.
    

    To solve the string vs integer problem, you can add an xs:choice field to your xsd, this will generate an “Item” which you can then cast as a Int or String as appropriate. Have a look at http://msdn.microsoft.com/en-us/library/exchange/bb426064(v=exchg.140).aspx

    <xs:complexType name="FindItemParentType">
      <xs:choice>
        <xs:element name="Items" type="t:ArrayOfRealItemsType"/>
        <xs:element name="Groups" type="t:ArrayOfGroupedIetmsType"/>
      </xs:choice>
      <xs:attributeGroup ref="t:FindResponsePagingAttributes"/>
    </xs:complexType>
    
    FindItemParentType fipt = new FindItemParentType();
    object obj = fipt.Item;
    if (obj is ArrayOfGroupedItemsType)
    {
        ArrayOfGroupedItemsType groupedItems = (obj as ArrayOfGroupedItemsType);
        //TODO: Write code to handle grouped items.
    }
    else if (obj is ArrayOfRealItemsType)
    {
        ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);
        //TODO: Write code to handle items.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone suggest a CMS able to offer the following features: Free/open source Support
Can anyone suggest a way to shave any time off this script? var countObject
Can anyone suggest me good plugin for memory leakage which can be integrated in
Can anyone suggest a good javascript library for displaying and navigating huge images in
Can anyone suggest some fast ways to calculate the lowest common factor(excluding 1) of
Can anyone suggest a book or a tutorial on creating Charts in PHP extracting
Can anyone suggest a good solution to remove duplicates from nested lists if wanting
Can anyone suggest some best options to include charting feature in my ASP.NET pages
Can anyone suggest me a good papervision3D book to read? I know as3 pretty
Can anyone suggest where can I find good starter videos for ASP.NET MVC (other

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.