使用 LINQ to XML、Enums 将 XML 中的数据提取到 C# 对象中
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
DescriptionAttribute
s 与 type 属性完全匹配XML 中的字符串可以使用反射。编辑:转换为通用
,然后像这样调用它:
If the
DescriptionAttribute
s matches exactly with the type attribute strings in the XML you can use reflection.Edit: convert to generic
and then call it like this: