Click here to Skip to main content
13,049,814 members (74,393 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 21 Jul 2007

Loading XML Data Using Generics and Reflection

, 21 Jul 2007
Rate this:
Please Sign up or sign in to vote.
A generic method that takes the XML node list and type and loads the XML data in a node of the supplied type.


If used properly, Generics and Reflection together can do magic. I am not a big magician but I have put up a small sample to read and load XML data generically.

Scenario: Say you have non-OLTP'ish data which you don't want to hard code in your application but place in XML (which I did) and load the data at the start of your application, may be to cache or use directly.

In this example I have two sets of data: site URLs (you don't need to do this, SiteMap in 2.0 is great, this is just for the sample) and states of different countries (here I am using those of USA).

The namespaces in use are System.xml (to load and XPath the XML) and System.Reflection.

private static Collection<T> Get<T>(XmlNodeList nodeList)
               where T : new()
    Collection<T> collOfT = new Collection<T>();

    foreach (XmlNode node in nodeList)
        T t = new T();

        PropertyInfo[] propertiesOfT = typeof(T).GetProperties();

        foreach (PropertyInfo property in propertiesOfT)
            string value = node.Attributes[property.Name].Value.ToString();

            property.SetValue(t, value, null);


    return collOfT;

The XML:

<?xml version="1.0"?>
            <Url Key = "Home" Path = "Home.htm"></Url>
            <Url Key = "AdminHome" Path = "Admin.htm"></Url>
            <Url Key = "ManagementHome" Path = "Manage.htm"></Url>

            <State Abbr="AL" Name ="Alabama" />
            <State Abbr="TX" Name ="Texas" />
            <State Abbr="NC" Name ="North Carolina" />
            <State Abbr="MS" Name ="Mississippi" />
            <State Abbr="WV" Name ="West Virginia" />


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


About the Author

Kiran Bheemarti
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralYAWA Pin
Paul A. Howes21-Jul-07 12:19
memberPaul A. Howes21-Jul-07 12:19 
GeneralRe: YAWA Pin
bakirankumar21-Jul-07 12:31
memberbakirankumar21-Jul-07 12:31 
GeneralRe: YAWA Pin
bakirankumar21-Jul-07 12:32
memberbakirankumar21-Jul-07 12:32 
QuestionWhy not use Xml De-serialization? Pin
malharone21-Jul-07 12:18
membermalharone21-Jul-07 12:18 

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 21 Jul 2007
Article Copyright 2007 by Kiran Bheemarti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid