Example #1
0
        private void ParseXMLTags(String text)
        {
            // HTML Regex from Phil Haack.
            Regex regex = new Regex(
                @"<"
                + @"(?<endTag>/)?"           //Captures the / if this is an end tag.
                + @"(?<tagname>\w+)"         //Captures TagName
                + @"("                       //Groups tag contents
                + @"(\s+"                    //Groups attributes
                + @"(?<attName>\w+)"         //Attribute name
                + @"("                       //groups =value portion.
                + @"\s*=\s*"                 // =
                + @"(?:"                     //Groups attribute "value" portion.
                + @"""(?<attVal>[^""]*)"""   // attVal='double quoted'
                + @"|'(?<attVal>[^']*)'"     // attVal='single quoted'
                + @"|(?<attVal>[^'"">\s]+)"  // attVal=urlnospaces
                + @")"
                + @")?"                      //end optional att value portion.
                + @")+\s*"                   //One or more attribute pairs
                + @"|\s*"                    //Some white space.
                + @")"
                + @"(?<completeTag>/)?>"     //Captures the "/" if this is a complete tag.
                , RegexOptions.IgnoreCase | RegexOptions.Compiled);
            MatchCollection matches = regex.Matches(text);

            if (matches.Count > 0)
            {
                int lasttagend = 0;
                // TODO: Make this a class variable.
                string previoustag = "";
                for (int i = 0; i < matches.Count; i++)
                {
                    Match  match  = matches[i];
                    string value  = match.Value;
                    int    index  = match.Index;
                    int    length = match.Length;
                    try
                    {
                        switch (value)
                        {
                        case "</zone>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _mapDlg.ParseZoneText(usabletext);
                        }
                        break;

                        case "<zone>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</room>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _mapDlg.ParseRoomText(usabletext);
                        }
                        break;

                        case "<room>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</equipment>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _equipmentDlg.ParseText(usabletext);
                        }
                        break;

                        case "<equipment>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</prompt>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _statusDlg.ParseText(usabletext);
                        }
                        break;

                        case "<prompt>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</exits>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _mapDlg.ParseExitText(usabletext);
                        }
                        break;

                        case "<exits>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</map>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _mapDlg.ParseMapText(usabletext);
                        }
                        break;

                        case "<map>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        case "</roomDescription>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            _mapDlg.ParseRoomDescriptionText(usabletext);
                        }
                        break;

                        case "<roomDescription>":
                        {
                            string usabletext = text.Substring(lasttagend, index - lasttagend);
                            ProcessWindowText(usabletext);
                        }
                        break;

                        default:
                            string unrecognizedtag = value;
                            break;
                        }
                    }
                    catch (IndexOutOfRangeException ex)
                    {
                        string str = ex.ToString();
                    }
                    lasttagend  = index + length;
                    previoustag = value;
                }
                // TODO: If the last tag in the string was NOT a closing tag and this is within an existing tag,
                // save this text until we do get a closing tag.
                if (lasttagend < text.Length)
                {
                    ProcessWindowText(text.Substring(lasttagend));
                }
            }
            else
            {
                ProcessWindowText(text);
            }
        }