Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating a simple XML Parser alternative to LINQ

0.00/5 (No votes)
30 Oct 2010 1  
Creating a simple XML Parser alternative to LINQ Here is a simple way to parse an XML string, and store its element name and values in a Hashtable

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Creating a simple XML Parser alternative to LINQ

Here is a simple way to parse an XML string, and store its element name and values in a Hashtable which you can call its key using the GetAttribute() method.

    public class Parse
    {
        Hashtable attributes;

        public void ParseURL(string xmlData)
        {
            try
            {
                string errorString = string.Empty;
                byte[] byteArray = new byte[xmlData.Length];
                System.Text.ASCIIEncoding encoding = new
                System.Text.ASCIIEncoding();
                byteArray = encoding.GetBytes(xmlData);
                attributes = new Hashtable();

                // Load the memory stream
                MemoryStream memoryStream = new MemoryStream(byteArray);

                memoryStream.Seek(0, SeekOrigin.Begin);

                XmlTextReader reader = new XmlTextReader(memoryStream);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            string strURI = reader.NamespaceURI;
                            string strName = reader.Name;
                            if (reader.HasAttributes)
                            {
                                for (int i = 0; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToAttribute(i);
                                    if (!attributes.ContainsKey(reader.Name))
                                    {
                                        attributes.Add(reader.Name, reader.Value);
                                    }
                                }
                            }

                            break;

                        default:
                            break;
                    }
                }
            }
            catch (XmlException e)
            {
                Console.WriteLine("error occured: " + e.Message);
            }

        }

        public string GetAttribute(string key)
        {
            try
            {
                return attributes[key].ToString();
            }
            catch (XmlException e)
            {
                return "error occured: " + e.Message;
            }
        }

  }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here