Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<?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.
Posted
Updated 6-May-13 20:26pm
v2
Comments
Per Söderlund 7-May-13 2:13am    
So what, do you need to load from Xml into a struct or do you need to load from struct into xml?

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
 
Share this answer
 
Comments
Pratik Gaikwad 7-May-13 14:48pm    
hi Rasool_Ahmed/Sergey Alexandrovich Kryukov,

Thank you for quick response.

@Sergey Alexandrovich Kryukov,

I understand it is difficult to implement.I was able to solve the problem using reflection but there was a trade out.
For only single object, it is roughly taking 4ms to process. I still want to use some other method which will reduce the processing time.
Can you suggest anything better for this?
Sergey Alexandrovich Kryukov 7-May-13 14:52pm    
Existing serialization. For best flexibility, ease of use, good maintenance, backup compatibility, use Data Contract. For better performance, use not XML, but serialization with BinaryFormatter:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx

—SA
Pratik Gaikwad 8-May-13 1:19am    
Thank you again. But right now reading XML file is not a major concern. what I need to know is how to extract property values in optimized way?
Sergey Alexandrovich Kryukov 8-May-13 1:40am    
In my strong and well argumented opinion (please see my past answers referenced above), there is nothing better then Data Contract. You don't work with XML by yourself. You don't need to modify your data types, you only add some attributes, which is totally unobtrusive: you don't need to implement any interfaces, inherit certain base classes, you don't even need to make persistent members public. You can store any object graph (not even a tree, but anything), and later restore it in memory as it was at the previous run. The approach it totally agnostic to the concrete data type and yet very efficient as it is based on System.Reflection.Emit: the types are "reflected" only once, serialization code is generated on the fly, and later reused.

I strongly suggest you accept this solution and employ it.
—SA
Pratik Gaikwad 9-May-13 1:05am    
Thank you again. I will definitely be applying Serialization based Reflection. One thing I need to know about it is, can we used it at UI side? If yes then can you please give me some sample codes I can use as reference?
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900