Click here to Skip to main content
11,490,292 members (54,589 online)
Click here to Skip to main content
Add your own
alternative version

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

, 18 Oct 2005 43.1K 448 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.Evaluated;
using System.Xml.XPath;
using System.Xml;
using Comnicate.CodeDom.Xml.ParserSchemas.Rules.Evaluation;

namespace Comnicate.CodeDom.Xml.ParserSchemas.Rules
{
    /// <summary>
    /// Represents a rule matching an element and its child rules. You can specify child rules (nodes) of the element and required some of them.
    /// </summary>
    public class ElementRule : ParentalRule
    {
        #region .ctors

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementRule"/> class.
        /// </summary>
        /// <param name="elementName">Element name.</param>
        public ElementRule(string elementName) : base(elementName) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementRule"/> class.
        /// </summary>
        /// <param name="elementName">Element name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        public ElementRule(string elementName, Uri namespaceUri) : base(elementName, namespaceUri) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ElementRule"/> class.
        /// </summary>
        /// <param name="elementName">Element name.</param>
        /// <param name="namespaceUri">Namespace Uri.</param>
        /// <param name="options">Node value handling options.</param>
        public ElementRule(string elementName, Uri namespaceUri, NodeValueOptions options)
            : base(elementName, namespaceUri, options)
        {
            // Default value
            this.OptionalEntriesMatchOption = OptionalEntriesMatchOption.DoNotRequireAnyOptionalEntries;
        }        

        #endregion        

        #region Properties

        /// <summary>
        /// Gets the information about the type of node navigator that can be matched by this rule.
        /// </summary>
        public override XPathNodeType MatchingNodeType
        {
            get { return XPathNodeType.Element; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Parses node navigator the XPathNavigator instance is pointed to, according to the rule.
        /// </summary>
        /// <param name="nodeNavigator">XPathNavigator instance pointing to the node to parse.</param>
        /// <returns>Evaluated rule.</returns>
        public override EvaluatedParentalRule Evaluate(XPathNavigator nodeNavigator)
        {
            // Evaluated rule
            EvaluatedParentalRule result = new EvaluatedParentalRule(this);

            // Evaluate child rules
            this.EvaluateChildRules(nodeNavigator, result);

            // Return evaluated rule
            return result;
        }
            
        #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

Share

About the Author

Tomas Deml

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 | ;-)

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 18 Oct 2005
Article Copyright 2005 by Tomas Deml
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid