开发者

Extract Data from XML into C# object using LINQ to XML, Enums

I am trying to extract data from XML file and save it my C# class/object. My problem is

I have an XMl file like this

<personal_auto xmlns = "http://cp.com/rules/client"> 
 <claim id = "s1" type = "Subject Section">
    <report > 
    </report> 
    <policy>
    </policy>
 </claim>
 <claim id = "s2" type = "Vehichle Section">
    <report >
    </report>
    <policy>
    </policy>
  </claim>
  <claim id = "s3" type = "Agent Section">>
    <report 
    </report>
    <policy>
    </policy>
  </claim>
</personal_auto> 

I have 开发者_高级运维an enum like this

    public enum typesectionEnum
    {
        [Description("Subject Section")]
        subjectSection,
        [Description("Vehicle Section")]
        vehicleSection,
        [Description("Possible Related Section")]
        possibleRelatedSection,
        [Description("Agent (Summary) Section")]
        AgentSection
    }

I am trying to extract data from the XML file and save to my C# class/object.

List<claim> = ( from d in query.Descendants(xmlns + "claim")
                 select new Claim 
                   {
                    id = d.Attribute("id").value,
                    type = ????                    
                    }
                 ).ToList (),

What I am wondering is, I want to set the value in my application that will access the value in the xml file.


If the DescriptionAttributes matches exactly with the type attribute strings in the XML you can use reflection.

Edit: convert to generic

public TEnum GetEnum<TEnum>(string input) where TEnum : struct
{
    if (!typeof(TEnum).IsEnum)
        throw new Exception(typeof(TEnum).GetType() + " is not en enum");
    Type dataType = Enum.GetUnderlyingType(typeof(typesectionEnum));
    foreach (FieldInfo field in
        typeof(typesectionEnum).GetFields(BindingFlags.Static | BindingFlags.GetField |
        BindingFlags.Public))
    {
        object value = field.GetValue(null);
        foreach (DescriptionAttribute attrib in field.GetCustomAttributes(true).OfType<DescriptionAttribute>())
        {
            if (attrib.Description == input)
            {
                return (TEnum)value;
            }
        }
    }
    return default(TEnum);
}

and then call it like this:

select new Claim 
{
    id = d.Attribute("id").value,
    type = GetEnum<typesectionEnum>(d.Attribute("type").value),
}


List<claim> claims = ( 
    from d in query.Descendants(xmlns + "claim") 
    let t = d.Attribute("type").Value
    select new Claim  
    { 
        id = d.Attribute("id").value, 
        type =  t == "Subject Section" ? typesectionEnum.subjectSection :
                (t == "Vehicle Section" ? typesectionEnum.vehicleSection :
                 (t == "Possible Related Section" ? typesectionEnum.possibleRelatedSection :
                                                    typesectionenum.AgentSection))
     }
 ).ToList ();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜