Click here to Skip to main content
13,052,900 members (62,923 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 16 Nov 2009

Reflection to Read XML

, 16 Nov 2009
Rate this:
Please Sign up or sign in to vote.
How Reflection can be used to populate XML data to BO


By deserializing XML file, data can be loaded into objects. Suppose you have a legacy application that saves data as an XML file, and you have a new application whose structure is entirely different from the old application. But the new application should be able to load the old application files. In this case, deserialization would not be a solution.

Using Reflection and XmlReader, old XML files can be populated to new application objects.

Using the Code

The sample application can import an old employee file, create a new employee file, and load the selected file. The following figure shows an imported employee file.

Adding and loading files are common functionality. Let us see how importing an old file into a new class structure works!!!

First, we have to map old properties to new properties.This is done by Mapper.xml.
Following mapper file maps ,"EmployeeID","EmployeeName" and "EmployeeSalary" properties to new properties. Object information is also included. Type is required in case you have user defined data types.

  <EmployeeID Object="EmployeeBO" Property="ID" Type="String" />
  <EmployeeName Object="EmployeeBO" Property="Name" Type="String" />
  <EmployeeSalary Object="EmployeeBO" Property="Salary" Type="String" />

Now it is time to read the XML.
This is done by XMLReader. When element/attribute is read, use this element/attribute as XPath to retrieve respective object and property name from Mapper.xml. Using this object information and reflection, get property information and copy the value to new property.
The following code explains the same.

That's it!!! Your old file is migrated to a new application.

public void LoadXMLToBO(Stream  stream, object dataObject)
      if (stream !=null && dataObject != null)
          XmlReader reader = XmlReader.Create(stream);
          while (reader.Read())
              switch (reader .NodeType)
                  case XmlNodeType.Element:
                      while (reader.MoveToNextAttribute())
                          ProcessXmlData(reader.Name, reader.Value, dataObject);
                  case XmlNodeType.Text:
                      ProcessXmlData(reader.Name, reader.Value, dataObject);
private  void ProcessXmlData(string strNodeName,string strValue,object dataObject)
       if (!string.IsNullOrEmpty(strNodeName) && !string.IsNullOrEmpty(strValue))
             //use node name as XPath
         XmlNode xNode= mapperDoc.SelectSingleNode("Mapper//" + strNodeName);
          if (xNode != null)
            switch (xNode.Attributes["Object"].Value)
               //convert data object to EmployeeBO
               case "EmployeeBO":
                 EmployeeBO employeeBO = dataObject as EmployeeBO;
                 SetValue(xNode.Attributes["Property"].Value, strValue, dataObject);
private void SetValue(string strPoperty,string strValue,object dataObject)
      //Find property by reflection
      PropertyInfo property = FindProperty(strPoperty, dataObject);
      if (property != null)
          property.SetValue(dataObject, strValue, null);
private  PropertyInfo FindProperty(string strProperty, object dataObject)
     PropertyInfo property = null;
     if (!string.IsNullOrEmpty(strProperty) && dataObject != null)
         Type objDataType = dataObject.GetType();
         List<PropertyInfo> objPropInfo = 
         for (int i = 0; i < objPropInfo.Count; i++)
           if (objPropInfo == null) continue;
           if (objPropInfo[i].Name == strProperty)
                property = objPropInfo[i];
        return property;

Points of Interest

Instead of XMLReader, XMLDocument can also be used.
In switch case of "ProcessXmlData", you can include other objects as well depending on to the object the property belongs to. That is the reason Mapper.xml has "Object" attribute.


  • Initial version


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


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Mark Nischalke18-Nov-09 4:53
mentorMark Nischalke18-Nov-09 4:53 
GeneralRe: My vote of 1 Pin
KN.Sudha19-Nov-09 0:23
memberKN.Sudha19-Nov-09 0:23 
GeneralThoughts Pin
PIEBALDconsult17-Nov-09 3:49
memberPIEBALDconsult17-Nov-09 3:49 
GeneralRe: Thoughts Pin
KN.Sudha19-Nov-09 0:14
memberKN.Sudha19-Nov-09 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 17 Nov 2009
Article Copyright 2009 by KN.Sudha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid