Exemplo n.º 1
0
        public bool Parse(out XmlErrorCollection errorCollection, out SchemeGenerator schemeGenerator)
        {
            if (_xDoc == null)
            {
                throw new ArgumentNullException("XML document is not valid.");
            }

            //Store a reference to a new scheme generator and error collector for ease of use later on.
            //This object is not responsible for their lifetime once Parse() has finished!
            _schemeGenerator = new SchemeGenerator();
            _errorCollection = new XmlErrorCollection();

            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = _xDoc.Elements();

            foreach (XElement element in elements)
            {
                //Handle scheme element.
                if (element.Name.LocalName == ElementTypes.Metascheme.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Metascheme))
                    {
                        foundElements.Add(ElementTypes.Metascheme, element);
                        ParseMetaschemeElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }

            //If we didn't get the scheme element, we didn't do anything just now!
            if (!foundElements.Contains(ElementTypes.Metascheme))
            {
                _errorCollection.AddElementNotFound(ElementTypes.Metascheme.ToString());
            }

            //Set the out variables and lose our stored references to them.
            schemeGenerator  = _schemeGenerator;
            _schemeGenerator = null;

            errorCollection  = _errorCollection;
            _errorCollection = null;

            return(errorCollection.Errors.Count == 0);
        }