如何使用XML中的属性从对象动态提取数据 [英] How to extract data dynamically from object using properties in XML

查看:77
本文介绍了如何使用XML中的属性从对象动态提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要帮助开发一个逻辑,用于从XML文件中提到的属性中提取自定义对象的数据。



例如,



让我们自定义课程:客户。

此客户类具有以下属性:客户编号,名称,地址,电话等。



现在XML文件看起来像



 <?  xml  < span class =code-summarycomment>   version   =  1.0    encoding   =   utf-8    >  
< 客户 >
< CustomerNumber displayName = 客户编号 < span class =code-attribute> ListOrder = 1 > Customer.Number < / CustomerNumber >
< 名称 displayName = 名称 ListOrder = 2 > Customer.Name < / Name >
< 地址 displayName = 地址 ListOrder = 3 > Customer.Address < /地址 >
< 电话 displayName = 电话 ListOrder = 4 > Customer.Phone < /电话 >
< / Customer >







我将从另一个项目接收客户对象。



我需要知道的是,如何使用上面的Customer对象提取数据XML文件。



例如,

如果当前在XMLReader中,Customer.Name存在,那么我有来自Customer Object的Name我有收到。



有人可以帮我吗?



提前致谢。





PS上面的XML可以根据需要进行修改。

解决方案

请参阅方法 System.Type.GetProperty :< a href =http://msdn.microsoft.com/en-us/library/system.type.aspx> http://msdn.microsoft.com/en-us/library/system.type.aspx [ ^ ]。



然而,你正在进入一个很难以通用方式实现的解决方案,而且性能会很低,因为反复使用反射是太贵了。真正的解决方案(基于反射的序列化)要复杂得多:反射只使用一次;在运行时生成整个序列化程序集并在运行时重用。要实现类似的东西,你应该具备技术架构方面的非平凡技能和CIL的良好知识,这也很难调试。



为什么不使用这个问题的可用解决方案称为数据合同?请参阅: http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



请参阅我过去提倡这种方法的答案:

如何在我的表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [ ^ ]。



-SA


您必须先将XML转换为Class,请访问此链接



然后使用BinaryFormater和MemoryStream将其解析为Object,请访问链接



祝你好运。


Hi all,

I need help in developing a logic for extracting data from a custom object for properties mentioned in XML file.

For example,

Let''s take a custom class : Customer.
This customer class has properties : Customer Number, Name, Address, Phone, etc.

Now the XML file looks like

<?xml version="1.0" encoding="utf-8" ?>
<Customer>
  <CustomerNumber displayName="Customer Number" ListOrder="1">Customer.Number</CustomerNumber>
  <Name displayName="Name" ListOrder="2">Customer.Name</Name>
  <Adress displayName="Address" ListOrder="3">Customer.Address</Adress>
  <Phone displayName="Phone" ListOrder="4">Customer.Phone</Phone>
</Customer>




I will be receiving Customer Object from another project.

What i need to know is, how can I extract the data from Customer object using above XML file.

For example,
If currently in XMLReader, Customer.Name is present then I have Name from Customer Object which I have received.

Can anyone help me please?

Thanks in advance.


P.S. Above XML can be modified as per needs.

解决方案

Please see the methods System.Type.GetProperty: http://msdn.microsoft.com/en-us/library/system.type.aspx[^].

However, you are getting into a solution which is pretty hard to implement in a universal way, and the performance is going to be low, because using reflection over and over is too expensive. The real solutions (reflection-based serialization) is much more complex: reflection is used only once; a whole serialization assembly is generated on the fly and reused during runtime. To implement something like that, you should have non-trivial skills in technology architecture and good knowledge of CIL, which is also pretty hard to debug.

Why not using the available solution of this problem called Data Contract? Please see: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see also my past answers advocating this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


You must first convert your XML to Class, visit this link

And then parse it to Object using BinaryFormater and MemoryStream, visit this link

Good luck.


这篇关于如何使用XML中的属性从对象动态提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆