将xml值映射到枚举类型

Ger*_*ard 15 c# xml enums

我需要解析从第三方到C#对象的XML文件.我收到的一些XML具有枚举值,我想将其存储在枚举类型中.

例如,我有以下xml文件的xsd:

<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>  
Run Code Online (Sandbox Code Playgroud)

我想把它映射到枚举,我得到了这个:

public enum Fuel
{
    B,
    D,
    L,
    E,
    H,
    C,
    O
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是xml可以包含一个3我似乎无法放入枚举类型的值.是否有任何解决方案将此值放入枚举中.

我也可以使用a -或a /中的其他值获取其他值,并且我想将其放入枚举类型中.
欢迎Anu建议!

And*_*rew 24

使用以下XmlEnum属性进行装饰:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx

public enum Fuel
{
    [XmlEnum("B")]
    Benzine,
    [XmlEnum("D")]
    Diesel,
    [XmlEnum("L")]
    LpgGas,
    [XmlEnum("3")]
    LpgG3,
    [XmlEnum("E")]
    Elektrisch,
    [XmlEnum("H")]
    Hybride,
    [XmlEnum("C")]
    Cryogeen,
    [XmlEnum("O")]
    Overig
}
Run Code Online (Sandbox Code Playgroud)

  • 一直撕裂我的脑袋,以了解为什么我不能标记类字段,因为[XmlEnum]终于找到了你的答案,并保存了自己剩下的头发.如果你问我,所有其他选择都是"脏的" (4认同)

Dar*_*mas 19

您可以使用以下命令将xml属性值解析回枚举类型:

var value = Enum.Parse(typeof(Fuel), "B");
Run Code Online (Sandbox Code Playgroud)

但我不认为你会得到真正的远与你的"特殊"的值(3,a/等).你为什么不把你的枚举定义为

enum Fuel
{
    Benzine,
    Diesel,
    // ...
    Three,
    ASlash,
    // ...
}
Run Code Online (Sandbox Code Playgroud)

并编写一个静态方法将字符串转换为枚举成员?

您可以考虑实现这样一个方法的一件事是将自定义属性添加到包含其字符串表示形式的枚举成员中 - 如果值在枚举中没有精确的对应项,则查找具有该属性的成员.

创建这样的属性很简单:

/// <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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的枚举中使用它们:

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

这两种方法将帮助您将字符串解析为您选择的枚举成员:

    /// <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;
    }
Run Code Online (Sandbox Code Playgroud)

虽然我在这里:这是另一回事;)

    /// <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;

    }
Run Code Online (Sandbox Code Playgroud)

  • 问题是映射非标准枚举值(如"3"). (2认同)

ja7*_*a72 5

为什么你不能解析字符串

[XmlAttribute("brandstof")]
public string FuelTypeString
{
    get { return fuel.ToString(); }
    set
    {
        fuel = (Fuel)System.Enum.Parse(typeof(Fuel), value);
    }
}
[XmlIgnore()]
public Fuel FuelType
{
    get { return fuel; }
    set { fuel = value; }
}
Run Code Online (Sandbox Code Playgroud)

所以你真的将它序列化为一个字符串。