Click here to Skip to main content
Licence CPOL
First Posted 16 Nov 2009
Views 3,698
Downloads 100
Bookmarked 4 times

Reflection to Read XML

By | 16 Nov 2009 | Article
How Reflection can be used to populate XML data to BO

Introduction

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.

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

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())
            {
                reader.MoveToContent();
                switch (reader .NodeType)
                {                        
                    case XmlNodeType.Element:
                        while (reader.MoveToNextAttribute())
                        {
                            ProcessXmlData(reader.Name, reader.Value, dataObject);
                        }
                        break;                       
                    case XmlNodeType.Text:
                        ProcessXmlData(reader.Name, reader.Value, dataObject);
                        break;                       
                }
            }
        }
     } 
 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);
                  break;
             }                   
           }
        }
    } 
  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 = 
                  objDataType.GetProperties().ToList<PropertyInfo>();
         for (int i = 0; i < objPropInfo.Count; i++)
         {
           if (objPropInfo == null) continue;
           if (objPropInfo[i].Name == strProperty)
             {
                property = objPropInfo[i];
                 break;
             }
          }
        }
        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.

History

  • Initial version

License

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

About the Author

KN.Sudha



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmentorMark Nischalke4:53 18 Nov '09  
GeneralRe: My vote of 1 PinmemberKN.Sudha0:23 19 Nov '09  
GeneralThoughts PinmemberPIEBALDconsult3:49 17 Nov '09  
GeneralRe: Thoughts PinmemberKN.Sudha0:14 19 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Nov 2009
Article Copyright 2009 by KN.Sudha
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid