Click here to Skip to main content
15,903,203 members
Articles / Programming Languages / C#

Loading XML Data Using Generics and Reflection

Rate me:
Please Sign up or sign in to vote.
1.23/5 (4 votes)
21 Jul 2007CPOL 22.3K   141   7   4
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.

C#
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
<?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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYAWA Pin
Paul A. Howes21-Jul-07 12:19
Paul A. Howes21-Jul-07 12:19 
GeneralRe: YAWA Pin
Kiran Bheemarti21-Jul-07 12:31
Kiran Bheemarti21-Jul-07 12:31 
GeneralRe: YAWA Pin
Kiran Bheemarti21-Jul-07 12:32
Kiran Bheemarti21-Jul-07 12:32 
QuestionWhy not use Xml De-serialization? Pin
malharone21-Jul-07 12:18
malharone21-Jul-07 12:18 
Why not use Xml De-serialization? <eom>

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.