Exemple #1
0
        public void Load(Stream stream)
        {
            Decleration = new XmlTag(null);
            Root        = new XmlTag(null);

            using (var reader = new StreamReader(stream)) {
                while (!reader.EndOfStream)
                {
                    char c = reader.ReadChar();

                    if (c == '<')
                    {
                        if (reader.PeekChar() == '?')
                        {
                            Decleration.Parse(reader);
                        }
                        else
                        {
                            XmlTag newTag = new XmlTag(Root);
                            newTag.Parse(reader);
                            Root.Children.Add(newTag);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void PrintToConsole(XmlTag root, int depth)
        {
            string strStart = "";

            for (int i = 0; i < depth; i++)
            {
                strStart += "-- ";
            }

            foreach (XmlTag tag in root.Children)
            {
                if (tag.IsComment)
                {
                    Console.WriteLine(strStart + "(comment)");
                }
                else
                {
                    Console.WriteLine(strStart + tag.Name);
                }

                if (tag.Children.Count > 0)
                {
                    PrintToConsole(tag, depth + 1);
                }
            }
        }
Exemple #3
0
        public void Load(string strFilename)
        {
            Decleration = new XmlTag(null);
            Root        = new XmlTag(null);

            if (!File.Exists(strFilename))
            {
                return;
            }

            using (var reader = new StreamReader(File.OpenRead(strFilename))) {
                while (!reader.EndOfStream)
                {
                    char c = reader.ReadChar();

                    if (c == '<')
                    {
                        if (reader.PeekChar() == '?')
                        {
                            Decleration.Parse(reader);
                        }
                        else
                        {
                            XmlTag newTag = new XmlTag(Root);
                            newTag.Parse(reader);
                            Root.Children.Add(newTag);
                        }
                    }
                }
            }
        }
Exemple #4
0
        public XmlTag(XmlTag parent)
        {
            Name      = "";
            Value     = "";
            IsComment = false;

            Parent     = parent;
            Attributes = new Dictionary <string, string>();
            Children   = new List <XmlTag>();
        }
Exemple #5
0
        internal void IterateAllTags(XmlTag root, Action <XmlTag, int> callback, int depth)
        {
            foreach (XmlTag tag in root.Children)
            {
                callback(tag, depth);

                if (tag.Children.Count > 0)
                {
                    IterateAllTags(tag, callback, depth + 1);
                }
            }
        }
Exemple #6
0
 public XmlFile()
 {
     Decleration = new XmlTag(null);
     Root        = new XmlTag(null);
 }
Exemple #7
0
        internal void Parse(StreamReader fs)
        {
            bool bReadAttributes = true;
            bool bOpenTag        = false;

            while (true)
            {
                if (fs.PeekChar() == '?')
                {
                    // xml decleration is a small exception
                    fs.Expect("?xml ");
                    bReadAttributes = true;
                    break;
                }
                else if (fs.PeekChar() == '!')
                {
                    // xml comment
                    fs.Expect("!--");
                    IsComment = true;
                    var    strValue  = new StringBuilder();
                    string strEnding = "-->";
                    int    i         = 0;
                    while (!fs.EndOfStream)
                    {
                        char c = fs.ReadChar();
                        if (c == strEnding[i])
                        {
                            i++;
                        }
                        else
                        {
                            i = 0;
                            strValue.Append(c);
                        }
                        if (i == strEnding.Length)
                        {
                            break;
                        }
                    }
                    Value = strValue.ToString();
                    return;
                }
                else
                {
                    // xml tag
                    string strName = "";
                    // read the name of the tag, and get the character we ended with
                    char c = fs.ReadUntil(out strName, '\r', '\n', '\t', ' ', '>', '/');
                    Name = strName;
                    if (c == '/')
                    {
                        // if it's a slash, it's a closed tag
                        bReadAttributes = false;
                        bOpenTag        = false;
                    }
                    else if (c == '>')
                    {
                        // if it's a end-of-tag character, it's an open tag
                        bReadAttributes = false;
                        bOpenTag        = true;
                    }
                    break;
                }
            }

            // read attributes
            if (bReadAttributes)
            {
                var attrs = XmlHelpers.ParseAttributes(fs);
                Attributes = attrs.attributes;
                bOpenTag   = attrs.bOpenTag;
            }

            // if open tag
            if (bOpenTag)
            {
                // start reading the value
                var strValue = new StringBuilder();

                while (!fs.EndOfStream)
                {
                    char c = fs.ReadChar();
                    // check for tag
                    if (c == '<')
                    {
                        if (fs.PeekChar() == '/')
                        {
                            // end of this tag
                            fs.Expect("/" + Name + ">");
                            break;
                        }
                        else
                        {
                            // new tag nested in this tag
                            XmlTag newTag = new XmlTag(this);
                            newTag.Parse(fs);
                            Children.Add(newTag);
                        }
                    }
                    else
                    {
                        // add to value
                        strValue.Append(c);
                    }
                }

                // set value property
                Value = strValue.ToString();
                strValue.Clear();
            }
        }
Exemple #8
0
 public XmlTagDynamic(XmlTag t)
 {
     tag = t;
 }