Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / XML
Tip/Trick

Iterate XML with XMLReader

Rate me:
Please Sign up or sign in to vote.
1.50/5 (2 votes)
19 Dec 2011CPOL 23.3K   1   5
Ergonomic syntax to iterate through XML
**I am in process of reviewing the code as someone reported a potential bug, I have not verified this yet. I will make an update soon. Thanks for your patience -dec 19 - rj***

Our helper methods will create a dictionary of elements and dictionary of attributes. Therefore we simply loop through elements and attributes as so.

As always, with XML, there are 999 other ways to do the same thing however, this is simple syntax and a quick approach.

C#
Dictionary<string, string> elements = GetElements( xmlFragment );
            foreach ( var elementKeyPair in elements )
            {
                Dictionary<string, string> attributes = GetAttributes(elementKeyPair.Key, xmlFragment);
            }



C#
public static Dictionary<string, string> GetElements(string XmlFragment)
{
    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    byte[] byteArray = Encoding.ASCII.GetBytes(XmlFragment); //todo: optimize me
    MemoryStream stream = new MemoryStream(byteArray);        //todo: optimize me
    XmlReader reader = XmlReader.Create(stream);
    try
    {
        while (reader.Read())
        {
            if (reader.IsStartElement())
            {
                                       KeyValuePair<string, string> pair = new KeyValuePair<string, string>(reader.Name, reader.Value);
                if (dictionary.Contains(pair) == false)
                {
                    dictionary.Add(reader.Name, reader.Value);
                }
            }
        }
    }
    catch { }
    return dictionary;
}
public static Dictionary<string, string> GetAttributes(string element, string XmlFragement)
{
    Dictionary<string, string> dictionary = new Dictionary<string, string>();
    byte[] byteArray = Encoding.ASCII.GetBytes(XmlFragement); //todo: optimize me
    MemoryStream stream = new MemoryStream(byteArray);        //todo: optimize me
    XmlReader reader = XmlReader.Create(stream);

    while (reader.Read())
    {
        if (reader.IsStartElement())
        {
            if ( reader.Name == element )
            {
                for ( int i = 0; i < reader.AttributeCount; ++i )
                {
                    reader.MoveToNextAttribute();
                    dictionary.Add(reader.Name, reader.Value);
                }
            }
        }
    }
    return dictionary;
}

License

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


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 need more explains Pin
Yeurl_200715-Dec-11 22:17
Yeurl_200715-Dec-11 22:17 
Reason for my vote of 1
need more explains
GeneralRe: What do you want to know, I will add it. Pin
rj4516-Dec-11 7:38
rj4516-Dec-11 7:38 
GeneralReason for my vote of 2 off, nothing interesting Pin
alexxksys15-Dec-11 19:43
alexxksys15-Dec-11 19:43 
GeneralRe: First part of a series. Pin
rj4516-Dec-11 7:38
rj4516-Dec-11 7:38 
GeneralRe: sh*t Pin
Member 774276121-Dec-11 3:08
Member 774276121-Dec-11 3:08 

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.