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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:10:25+00:00 2026-06-04T17:10:25+00:00

Consider the following overly simplified chunk of XML: <ElementA> <AttributeValue DataType=http://www.w3.org/2001/XMLSchema#integer>5 </AttributeValue> </ElementA> Specifically,

  • 0

Consider the following overly simplified chunk of XML:

<ElementA>
   <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">5
   </AttributeValue>
</ElementA>

Specifically, looking at the AttributeValue element, from the DataType attribute, I know that my value is of type integer (though it could have been a double, string, datetime…any established datatype from the w3 standard). I would like to deserialize this xml to a .NET class with the strongly typed value. The first thing that came to my mind is to create a generic AttributeValue class:

public class AttributeValue<T>
{
    public T Value {get; set;}
}

but of course this won’t work for a couple of reasons – the biggest one being I would have to declare the type in the parent class which won’t compile because T is not defined:

public class ElementA
{
    public AttributeValue<T> {get; set; }  // Naturally, this will not work because T
}                                          // is not defined.

Plus, I would likely have to implement IXmlSerializable on my class to handle the custom serialization.

Is there a better way to solve this problem? I know I can serialize the DataType attribute in my code and store the value as a string, then convert later, but it would be helpful to actually have the correct type in my business object for later processing

Thanks for any help!

Jason

  • 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-04T17:10:27+00:00Added an answer on June 4, 2026 at 5:10 pm

    I appreciate your answer @caesay and I did implement it, but I’m not sure I require that type of functionality (to be able to add multiple properties to the dictionary). While I do use dynamo in dire need in my code, I try to avoid it where possible.

    Instead, I implemented the following structure to retain the generic type within my parent class:

    public class AttributeValueElement : XACMLElement
    {
    
        public AttributeValueElement()
            : base(XacmlSchema.Context)
        {
    
        }
    
        [XmlAttribute]
        public string DataType { get; set; }
    
    
        [XmlText]
        public string Value 
        { 
            get { return DataValue.GetValue().ToString(); }
            set
            {
                DataValue = AttributeValueFactory.Create(DataType, value);   
            }
        }
    
        public AttributeValue DataValue { get; set; }        
    }
    
    
    public abstract class AttributeValue
    {
        public AttributeValue()
        {
    
        }
        public abstract object GetValue();
    }
    
    public class AttributeValue<T> : AttributeValue
    {
        public T Value { get; set; }
        public override object GetValue()
        {
            return Value;
        }
    }
    

    And a corresponding factory class to create the attribute value:

    public static AttributeValue Create(string xacmlDataType, string value)
        {
    
            AttributeValue _attributeValue = null;
    
            switch (xacmlDataType)
            {
                case "http://www.w3.org/2001/XMLSchema#string":
                case "http://www.w3.org/2001/XMLSchema#x500Name":
                case "http://www.w3.org/2001/XMLSchema#ipAddress":
                case "http://www.w3.org/2001/XMLSchema#dnsName":
                case "http://www.w3.org/2001/XMLSchema#xPathExpression":
                    _attributeValue = new AttributeValue<string> { Value = value };
                    break;
                case "http://www.w3.org/2001/XMLSchema#boolean":
                    _attributeValue = new AttributeValue<bool> {Value = XmlConvert.ToBoolean(value) };
                    break;
                case "http://www.w3.org/2001/XMLSchema#integer":
                    _attributeValue = new AttributeValue<int> { Value = XmlConvert.ToInt32(value) };
                    break;
                case "http://www.w3.org/2001/XMLSchema#double":
                    _attributeValue = new AttributeValue<double> { Value = XmlConvert.ToDouble(value) };
                    break;
                case "http://www.w3.org/2001/XMLSchema#time":
                case "http://www.w3.org/2001/XMLSchema#date":
                case "http://www.w3.org/2001/XMLSchema#dateTime":
                    _attributeValue = new AttributeValue<DateTime> { Value = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Utc) };
                    break;
                case "http://www.w3.org/2001/XMLSchema#anyURI":
                    _attributeValue = new AttributeValue<Uri> { Value = new Uri(value) };
                    break;
                case "http://www.w3.org/2001/XMLSchema#hexInteger":
                    _attributeValue = new AttributeValue<byte[]> { Value = Encoding.ASCII.GetBytes(value) };
                    break;          
                case "http://www.w3.org/2001/XMLSchema#dayTimeDuration":
                case "http://www.w3.org/2001/XMLSchema#yearMonthDuration":
                    _attributeValue = new AttributeValue<TimeSpan> { Value = XmlConvert.ToTimeSpan(value) };
                    break;                
                default:
                    throw new NotImplementedException("Data type '" + xacmlDataType + "' is not a supported type.");
            }           
    
            return _attributeValue;
        }
    

    I hate to answer my own question on stackoverflow, but sometimes it happens.

    Thanks for the responses guys!

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

Sidebar

Related Questions

Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
Consider following XML document fragment: <Book> <Title>Example</Title> <Content> Some line </Content> <TOC> Again some
Consider following XAML: <Window x:Class=WpfApplication1.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <StackPanel> <TextBlock Text={Binding Dic[foo]}
Consider following schema: Customers: Col | Type | -------------------| id | INTEGER | name
Consider following code: ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(2134); aList.add(3423); aList.add(4234); aList.add(343); String tmpString
consider following url: http://sitename.com/School/Admin/PageViewer i have a subfolder in School named UserFiles that contains
Consider following two examples: Example 1: <xs:import namespace=http://example.com/ns schemaLocation=test.xsd/> Example2: <sample:Data Test=true xsi:schemaLocation=http://example.com/test.xsd> How
Consider following snippet code for xml. <rootnode> <child id=child1 ><![CDATA[child 1]]></child> <child id=child2 ><![CDATA[child
Consider following scenario: I have RESTful URL /articles that returns list of articles user

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.