Click here to Skip to main content
12,065,756 members (34,013 online)
Rate this:
Please Sign up or sign in to vote.
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" ?>
  <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>

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 6-May-13 20:57pm
Edited 6-May-13 21:26pm
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Please see the methods System.Type.GetProperty:[^].

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:[^].

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...[^].

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

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?
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.
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?
If can be used anywhere. As I say, Data Contract (DataContractSerialize, in particular) just takes your data type (you only mark types and persistent data members with attributes) and store/restore the whole object graph found in memory starting from some object. All your data types are reflected automatically and later reused. Apparently, it can be done with any data. If the data is related to UI, you can create data from UI and populate UI from data.

Even though this is not a rule, I however strongly recommend to have a data model isolated from anything else in the code; it should be a set of pure-data types. Your other code should have access to those data types, but your data types should remain totally agnostic to other code, to remain pure abstract data. You can use polymorphic sets, arrays, most kinds of collections, everything you can use to form an arbitrary object graph. In other words, you should maintain proper isolation of the layers in your code design.

If I answered all your questions, I think you can accept my solution formally (green button).
In all cases, your follow-up questions will be welcome.

Pratik Gaikwad 9-May-13 1:51am
Even if I do use this solution, to get the values from the properties I still need to use reflection, correct?
No, not at all. Data Contract (serialize class) does it all for you. You don't add any reflection code by yourself. Finally, please look at it. The usage is really simple, much simpler than any other serialization approach.

(It's not only Reflection, but also much more difficult to develop System.Reflection.Emit; a whole serialization assembly is created on the fly.)

Pratik Gaikwad 9-May-13 14:11pm
Hi SA, Can you please point me to some sample codes, which I can refer to develop my logic?Also you have posted two links from MSDN, can you please tell one more time which URL should I look?
I meant this one:

It looks like this article provides all further links and the basic code samples are provided in this articles as well as the related MSDN help pages.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160207.1 | Last Updated 7 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100