Click here to Skip to main content
15,884,717 members
Articles / Programming Languages / XML

Parser Schemas– Easy and Powerful parsing of XML-based languages

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
18 Oct 20058 min read 53.1K   495   44  
An article on parsing XML files according to the specified schema.
/***********************************************************************\
 * Comnicate.CodeDom.Xml.ParserSchemas                                 *
 * Parses xml-based languages according to a user defined schema.      *
 * Copyright � 2005 Tomas Deml (as Comnicate!)                         *
 *                  tomasdeml@msn.com                                  *
 *                                                                     *
 * This library is free software; you can redistribute it and/or       *
 * modify it under the terms of the GNU Lesser General Public          *
 * License as published by the Free Software Foundation; either        *
 * version 2.1 of the License, or (at your option) any later version.  *
 *                                                                     *
 * This library is distributed in the hope that it will be useful,     *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *
 * Lesser General Public License for more details.                     *
\***********************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules;
using System.Xml.XPath;

namespace Comnicate.CodeDom.Xml.ParserSchemas
{
    /// <summary>
    /// Represents a schema of a Xml document and allows you to parse and validate the document according to the schema.
    /// </summary>
    public class ParserSchema
    {
        #region Fields

        // Parent node
        private ParentalRule parentNode;

        #endregion

        #region Enums

        /// <summary>
        /// Specifies the node navigator the XPathNavigator points to.
        /// </summary>
        public enum ParentNodeMatchOption : int
        {
            /// <summary>
            /// The <see cref="ParentNodeMatchOption.NavigatorPointsToParentNode"/> option says that the XPathNavigator points to the node that can be matched with the <see cref="ParserSchema.ParentNode"/> rule, ie. parent node.
            /// </summary>
            NavigatorPointsToParentNode,

            /// <summary>
            /// The <see cref="ParentNodeMatchOption.NavigatorPointsToParentOfParentNode"/> option says that the XPathNavigator points to the node that is parent of the navigator that can be matched with the  <see cref="ParserSchema.ParentNode"/> rule, ie. parent node.
            /// </summary>
            NavigatorPointsToParentOfParentNode
        }

        #endregion

        #region .ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="ParserSchema"/> class.
        /// </summary>
        /// <param name="parentNodeRule">An instance of the <see cref="ParentalRule"/> class representing the parent node of the document.</param>
        public ParserSchema(ParentalRule parentNodeRule)
        {
            // Parent rule cannot be null
            if (parentNodeRule == null) throw new ArgumentNullException("parentNodeRule");

            // Init fields
            this.parentNode = parentNodeRule;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets (+ protected set) the parent node of the schema.
        /// </summary>
        public ParentalRule ParentNode
        {
            get
            {
                return this.parentNode;
            }
            protected set
            {
                this.parentNode = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Evaluates the schema and returns results.
        /// </summary>
        /// <param name="documentNavigator">Instance of the <see cref="XPathNavigator"/> class representing the Xml document.</param>
        /// <param name="matchOption">Specifies if the <paramref name="documentNavigator"/> points to the parent node that will be matched by the <see cref="ParentNode"/> rule or if it points to the parent of the parent node that will be matched by the <see cref="ParentNode"/> rule.</param>
        /// <returns>Evaluation eventData as an instance of the <see cref="EvaluatedParserSchema"/> class.</returns>
        public EvaluatedParserSchema Evaluate(XPathNavigator documentNavigator, ParentNodeMatchOption matchOption)
        {
            return EvaluatedParserSchema.EvaluateSchema(this, documentNavigator, matchOption);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Czech Republic Czech Republic
I'm a student of the Low-voltage Electrical Engineering specialized on Computing from the Czech republic.

I'm a C# kind of guy, fan of .NET.

I've formed a programming group called 'Comnicate!'. Currently the only member of the group is myself. Wink | ;-)

Comments and Discussions