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

  • Home
  • SEARCH
  • 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 6027053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:32:05+00:00 2026-05-23T04:32:05+00:00

I need to parse a XML file which I get from third party to

  • 0

I need to parse a XML file which I get from third party to C# objects.
Some of the XML I receive have enumeration values which I want to store in an enum type.

For example, I’ve got the following xsd of the xml file:

<xsd:simpleType name="brandstof">
  <xsd:restriction base="xsd:string">
    <!--  Benzine --> 
    <xsd:enumeration value="B" /> 
    <!--  Diesel --> 
    <xsd:enumeration value="D" /> 
    <!--  LPG/Gas --> 
    <xsd:enumeration value="L" /> 
    <!--  LPG G3 --> 
    <xsd:enumeration value="3" /> 
    <!--  Elektrisch --> 
    <xsd:enumeration value="E" /> 
    <!--  Hybride --> 
    <xsd:enumeration value="H" /> 
    <!--  Cryogeen --> 
    <xsd:enumeration value="C" /> 
    <!--  Overig --> 
    <xsd:enumeration value="O" /> 
  </xsd:restriction>
</xsd:simpleType>  

I want to map this to an enum and I got this far:

public enum Fuel
{
    B,
    D,
    L,
    E,
    H,
    C,
    O
}

The problem I have is that the xml can contain a value of 3 which I can’t seem to put in the enum type. Is there any solution to put this value in the enum.

I also can get other values with a - or a / in them and which I want to put in an enum type.
Anu suggestions are welcome!

  • 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-23T04:32:05+00:00Added an answer on May 23, 2026 at 4:32 am

    You can parse the xml attribute value back to an enum type with:

    var value = Enum.Parse(typeof(Fuel), "B");
    

    But I don’t think you will get really far with your “special” values (3, a/ etc.).
    Why don’t you define your enum as

    enum Fuel
    {
        Benzine,
        Diesel,
        // ...
        Three,
        ASlash,
        // ...
    }
    

    And write a static method to convert a string to an enum member?

    One thing you could look into for implementing such a method would be to add custom attributes to the enum members containing their string representation – if a value doesn’t have an exact counterpart in the enumeration, look for a member with the attribute.

    Creating such an attribute is easy:

    /// <summary>
    /// Simple attribute class for storing String Values
    /// </summary>
    public class StringValueAttribute : Attribute
    {
        public string Value { get; private set; }
    
        public StringValueAttribute(string value)
        {
            Value = value;
        }
    }
    

    And then you can use them in your enum:

    enum Fuel
    {
        [StringValue("B")]        
        Benzine,
        [StringValue("D")]
        Diesel,
        // ...
        [StringValue("3")]
        Three,
        [StringValue("/")]
        Slash,
        // ...
    }
    

    These two methods will help you parse a string into an enum member of your choice:

        /// <summary>
        /// Parses the supplied enum and string value to find an associated enum value (case sensitive).
        /// </summary>
        public static object Parse(Type type, string stringValue)
        {
            return Parse(type, stringValue, false);
        }
    
        /// <summary>
        /// Parses the supplied enum and string value to find an associated enum value.
        /// </summary>
        public static object Parse(Type type, string stringValue, bool ignoreCase)
        {
            object output = null;
            string enumStringValue = null;
    
            if (!type.IsEnum)
            {
                throw new ArgumentException(String.Format("Supplied type must be an Enum.  Type was {0}", type));
            }
    
            //Look for our string value associated with fields in this enum
            foreach (FieldInfo fi in type.GetFields())
            {
                //Check for our custom attribute
                var attrs = fi.GetCustomAttributes(typeof (StringValueAttribute), false) as StringValueAttribute[];
                if (attrs != null && attrs.Length > 0)
                {
                    enumStringValue = attrs[0].Value;
                }                       
    
                //Check for equality then select actual enum value.
                if (string.Compare(enumStringValue, stringValue, ignoreCase) == 0)
                {
                    output = Enum.Parse(type, fi.Name);
                    break;
                }
            }
    
            return output;
        }
    

    And while I’m at it: here is the other way round 😉

        /// <summary>
        /// Gets a string value for a particular enum value.
        /// </summary>
        public static string GetStringValue(Enum value)
        {
            string output = null;
            Type type = value.GetType();
    
            if (StringValues.ContainsKey(value))
            {
                output = ((StringValueAttribute) StringValues[value]).Value;
            }
            else
            {
                //Look for our 'StringValueAttribute' in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                var attributes = fi.GetCustomAttributes(typeof(StringValueAttribute), false);
                if (attributes.Length > 0)
                {
                    var attribute = (StringValueAttribute) attributes[0];
                    StringValues.Add(value, attribute);
                    output = attribute.Value;
                }
    
            }
            return output;
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a xml file which is practically an image of a
I have a collection of HTML documents for which I need to parse the
I need to parse Visual Studio automatically generated XML documentation to create a report.
For a one-shot operation, i need to parse the contents of an XML string
I have a rather big number of source files that I need parse and
I have an application running on multiple IIS servers that need to parse a
I have a string like this that I need to parse into a 2D
I have a query filter written in human readable language. I need to parse
I have a zip file which can contain any number of zipfiles inside it
So I'm trying to parse an xml file: <?xml version=1.0 encoding=utf-8 ?> <Root> <att1

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.