Exemplo n.º 1
0
        /// <summary>
        /// Creates an XML grammar which uses the base XML file passed in, and adds a rule for the list indicated.
        /// </summary>
        /// <param name="baseXmlFilePath">Absolute file path to the XML file tat we should use as the base.</param>
        /// <param name="ruleName">The name of the rule we are creating from the list.</param>
        /// <param name="list">List of values that the new rule may be. This list may be empty.</param>
        /// <returns>XmlGrammar instance which can be used to describe a grammar with the given base and the list of values in a rule.</returns>
        public static XmlGrammar CreateGrammarFromList(String baseXmlFilePath, String ruleName, List <String> list)
        {
            List <XmlElement> remoteElements = new List <XmlElement>();

            if (!list.Any())
            {
                // If the list is empty, the grammar file becomes invalid.
                // Therefore, I will add a nonsensical string that will never be detect
                // to prevent the grammar from erroring.
                // A better solution should be found for this, but it's not too important
                // right now.
                list.Add("XYZ123");
            }
            XmlDocument mainDoc = new XmlDocument();

            mainDoc.Load(baseXmlFilePath);
            XmlElement oneof = GrammarUtility.CreateListOfPossibleStrings(mainDoc, list);
            XmlElement rule  = GrammarUtility.CreateElement(mainDoc, "rule", new Dictionary <string, string>
            {
                { "id", ruleName },
                { "scope", "public" },
            });

            rule.AppendChild(oneof);
            mainDoc.LastChild.AppendChild(rule);
            return(new XmlGrammar(mainDoc));
        }