将XML值映射到枚举类型

18

我需要解析一个从第三方获取的XML文件,并将其转换为C#对象。有些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>  
我希望将此映射到枚举类型,我已经做了这么多工作:
public enum Fuel
{
    B,
    D,
    L,
    E,
    H,
    C,
    O
}

我的问题是XML中可能会包含一个值为3的枚举类型值,我似乎无法将其放入枚举类型中。是否有任何方法可以将此值放入枚举类型中?

我还可以获取其他包含-/的值,并希望将它们放入枚举类型中。
欢迎任何建议!


我点击下面的链接,发现它很有用。在C#中将枚举从整数反序列化 - Md Kauser Ahmmed
4个回答

28

5
一直费尽心思想要弄明白为什么我不能将类字段标记为[XmlEnum],最终找到了你的答案,解救了我余下的头发。如果问我,所有其他替代方案都是“不洁的”。 - Dany Khalife

20

您可以使用以下方式将XML属性值解析回枚举类型:

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

但我不认为你会用你的 "特殊" 值(3a/ 等)走得太远。 为什么不把你的枚举定义为

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

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

实现该方法的一种方式是向枚举成员添加自定义属性,包含它们的字符串表示形式 - 如果一个值在枚举中没有精确对应项,则查找带有该属性的成员。

创建这样的属性很容易:

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

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

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

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

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

顺带一提:这是另一种方式的相反情况 ;)

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

    }

2
问题在于映射非标准枚举值(如“3”)。 - Bala R

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

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


0
我创建了一个处理此事的类:
    class EnumCreator
    {
        public static Type CreateEnum(List<string> AValues)
        {
            // Get the current application domain for the current thread.
            AppDomain currentDomain = AppDomain.CurrentDomain;

            // Create a dynamic assembly in the current application domain, 
            // and allow it to be executed and saved to disk.
            AssemblyName aName = new AssemblyName("TempAssembly");
            AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
                aName, AssemblyBuilderAccess.RunAndSave);

            // Define a dynamic module in "TempAssembly" assembly. For a single-
            // module assembly, the module has the same name as the assembly.
            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");      

            // Define a public enumeration with the name "Elevation" and an 
            // underlying type of Integer.
            EnumBuilder eb = mb.DefineEnum("EnumValues", TypeAttributes.Public, typeof(int));

            // Define two members, "High" and "Low".
            foreach (string v in AValues)
            {
                eb.DefineLiteral(v, AValues.IndexOf(v));
            }

            // Create the type and save the assembly.
            Type finished = eb.CreateType();

            return finished;
        }
    }

你需要先读取XML文件并将值放入列表中,例如可以使用XElement对象来完成。

//编辑: 像这样:

XElement xml = XElement.parse("file.xml");
List<string> enumItems = new List<string>();

foreach(XElement row in   xml.Element("xsd:simpleType").Element("xsd:restriction").Elements())
{
  enumItems.Add(row.Attribute("value").Value);
}

Type Fuel = EnumCreator.CreatEnum(enumItems);

等等,这不是问题所在。@符号不能用吗?像@3、@/等等。我知道它可以与关键字如int和double一起使用。 - hcb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接