如何从 XML 元素创建类?
我在 Linq 查询中使用了一个匿名类型,并且希望将其设为一个类而不是匿名类型。
这些字段是:年龄和一个包含几个元素的 XML 节点。如何声明该类以便可以访问 XML 元素?
这是部分声明的类:
class Student {
int Age;
// ??? What to use here for the XML node? XElement?
}
I have an anonymous type used in a Linq query and want to make this a class instead of anonymous type.
The fields are: Age and an XML node that has a couple of elements. How do I declare the class so that I can access the XML elements?
Here's the partially declared class:
class Student {
int Age;
// ??? What to use here for the XML node? XElement?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您有一些类似这样的 XML:
并且您想在 ac# 类中表示它。我建议上2节课。 1 个用于学生,另一个用于内部节点。
在您的 Student 类中,您将具有以下属性:
然后您将能够执行 Student.Inner.Node1。
From what I understand is you have some XML something like this:
and you want to represent this in a c# class. I would suggest having 2 classes. 1 for the Student and then another for the innerNode.
In your Student class you will have the properties:
Then you will be able to do Student.Inner.Node1.
也许这就是您想要的..
为了解释,我创建了一个示例..
我创建了看起来像这样的学生课程,
我创建了课程课程。这个类将由 xml 中的值启动,
现在看看代码...
我希望它对您有帮助
Maybe this is what you looking for..
To explain i have created an example..
I have created student class that look like this
I created Course class. this class will be initiate by the values from the xml
now look the code...
I hope it help you