Exemple #1
0
            public XMLtag(string _params, XMLtag _parent)
            {
                bool withinQuotationMarks = false;

                int previousSplitPosition = 0;

                List <string> rawParams = new List <string>();

                for (int i = 0; i < _params.Length; i++)
                {
                    if (_params[i] == '\"' && !(i > 0 && _params[i - 1] == '\\'))
                    {
                        withinQuotationMarks = !withinQuotationMarks;
                    }

                    if (!withinQuotationMarks && _params[i] == ' ')
                    {
                        rawParams.Add(_params.Substring(previousSplitPosition, i - previousSplitPosition));
                        previousSplitPosition = i;
                    }
                }

                rawParams.Add(_params.Substring(previousSplitPosition, _params.Length - previousSplitPosition));              //because otherwise the last one wouldn't be caught

                for (int i = rawParams.Count - 1; i >= 0; i--)
                {
                    if (rawParams[i].Trim() == "")
                    {
                        rawParams.RemoveAt(i);
                    }
                }

                name = rawParams[0];

                parent = _parent;

                /* if (parent != null) {
                 *   Console.WriteLine("new tag: " + name + ", child of " + parent.name);
                 * }
                 * else {
                 *   Console.WriteLine("new tag: " + name + " at the root level");
                 * }*/

                myParams = new ParamKeyValuePair[rawParams.Count - 1];

                for (int i = 1; i < rawParams.Count; i++)
                {
                    myParams[i - 1] = new ParamKeyValuePair(rawParams[i]);
                    Console.WriteLine(myParams[i - 1].key);
                }
            }
Exemple #2
0
        public XML(string input)
        {
            input = input.Trim();
            int      pos      = 0;
            ReadMode readMode = ReadMode.WAITING_FOR_TAG;

            XMLtag currentTag = null;

            int startOfIntraTagData = 0;

            while (pos < input.Length)
            {
                switch (readMode)
                {
                case ReadMode.WAITING_FOR_TAG:

                    if (input[pos] != '<')
                    {
                        pos++;
                    }
                    else
                    {
                        readMode = ReadMode.READING_TAG;

                        if (currentTag != null && pos - startOfIntraTagData > 0)
                        {
                            currentTag.data = input.Substring(startOfIntraTagData, pos - startOfIntraTagData).Trim();
                            if (currentTag.data == "")
                            {
                                currentTag.data = null;
                            }
                        }
                        continue;
                    }
                    break;

                case ReadMode.READING_TAG:

                    pos++;     //skip opening '<'
                    int  TagDataStartPos      = pos;
                    int  TagDataLength        = 0;
                    bool withinQuotationMarks = false;

                    while (!(input[pos] == '>' && !withinQuotationMarks))       //until we reach the end of the tag and have left quotation marks
                    {
                        if (input[pos] == '\"')
                        {
                            withinQuotationMarks = !withinQuotationMarks;
                        }
                        pos++;
                        TagDataLength++;
                    }
                    pos++;

                    //test what kind of tag this is

                    if (input[TagDataStartPos] == '?')
                    {
                        currentTag = new XMLtag(input.Substring(TagDataStartPos + 1, TagDataLength - 2), currentTag);

                        if (currentTag.parent == null)       //then we are at the root level
                        {
                            tags.Add(currentTag);
                        }
                        else
                        {
                            currentTag.parent.children.Add(currentTag);
                        }
                        currentTag = currentTag.parent;     //just go back to the parent because a tag with '?' as the first char never has any children
                    }
                    else if (input[TagDataStartPos] == '/') //end the current tag and go back up
                    {
                        currentTag = currentTag.parent;
                    }
                    else
                    {
                        bool goUp = false;

                        if (input[TagDataStartPos + (TagDataLength - 1)] == '/')      //a tag with no children
                        {
                            currentTag = new XMLtag(input.Substring(TagDataStartPos, TagDataLength - 1), currentTag);
                            goUp       = true;
                        }
                        else
                        {
                            currentTag = new XMLtag(input.Substring(TagDataStartPos, TagDataLength), currentTag);
                        }

                        if (currentTag.parent == null)        //then we are at the root level
                        {
                            tags.Add(currentTag);
                        }
                        else
                        {
                            currentTag.parent.children.Add(currentTag);
                        }

                        if (goUp)       //this was a one-and-done tag and ended itself, so return to the parent
                        {
                            currentTag = currentTag.parent;
                        }
                    }

                    readMode            = ReadMode.WAITING_FOR_TAG;
                    startOfIntraTagData = pos;
                    break;
                }
            }
            Console.WriteLine("Finished parsing XML");
        }