process an xml element when reading an xml stream - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

process an xml element when reading an xml stream

Demo Code

// FreeBSD License
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.Linq;
using System.IO;//ww w.j  ava 2 s.com
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// process an xml element when reading an xml stream
        /// </summary>
        /// <param name="reader">the reader</param>
        /// <param name="elementName">the name of the element we are processing</param>
        /// <param name="subElementProcessor">a delegate to process any sub elements detected</param>
        public static void ProcessElement(XmlReader reader, string elementName, SubElementProcessor subElementProcessor)
        {
            if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == elementName)
            {
                reader.Read(); // Skip ahead to next node
                var element = reader.MoveToContent();
                while (element != XmlNodeType.None)
                {
                    if (element == XmlNodeType.EndElement && reader.LocalName == elementName)
                    {
                        break;
                    }
                    if (reader.IsStartElement())
                    {
                        subElementProcessor(reader);
                    }
                    reader.Read();
                    element = reader.MoveToContent();
                }
            }
        }
}

Related Tutorials