使用 LINQ to XML、Enums 将 XML 中的数据提取到 C# 对象中

发布于 2024-09-24 01:51:02 字数 1380 浏览 4 评论 0原文

我正在尝试从 XML 文件中提取数据并将其保存在我的 C# 类/对象中。我的问题是

我有一个像这样的 XMl 文件

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

我有一个像这样的枚举

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

我试图从 XML 文件中提取数据并保存到我的 C# 类/对象中。

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

我想知道的是,我想在我的应用程序中设置将访问 xml 文件中的值的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-10-01 01:51:02

如果 DescriptionAttributes 与 type 属性完全匹配XML 中的字符串可以使用反射。

编辑:转换为通用

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

,然后像这样调用它:

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

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),
}
久光 2024-10-01 01:51:02
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 ();
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 ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文