Click here to Skip to main content
Click here to Skip to main content

Loading XML Data Using Generics and Reflection

, 21 Jul 2007 CPOL
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.

Introduction

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);
        }

        collOfT.Add(t);
    }

    return collOfT;
}

The XML:

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

    <Country>
        <USA>
            <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" />
        </USA>
    </Country>
</Data>

License

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

Share

About the Author

Kiran Bheemarti
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralYAWA PinmemberPaul A. Howes21-Jul-07 13:19 
GeneralRe: YAWA Pinmemberbakirankumar21-Jul-07 13:31 
GeneralRe: YAWA Pinmemberbakirankumar21-Jul-07 13:32 
QuestionWhy not use Xml De-serialization? Pinmembermalharone21-Jul-07 13:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 21 Jul 2007
Article Copyright 2007 by Kiran Bheemarti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid