Ejemplo n.º 1
0
        public void Load(string strFilename)
        {
            Decleration = new XmlTag(null);
            Root        = new XmlTag(null);

            if (!File.Exists(strFilename))
            {
                throw new FileNotFoundException("XML file not found", strFilename);
            }

            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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
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 == '/' && fs.PeekChar() == '>')
                    {
                        // if it's "/>", it's a closed tag
                        bReadAttributes = false;
                        bOpenTag        = false;
                        fs.Expect('>');
                    }
                    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();
                var strValueTextNodes = new StringBuilder();

                // start reading text nodes
                var tagTextNode = new XmlTag(this);
                tagTextNode.IsTextNode = true;

                while (!fs.EndOfStream)
                {
                    char c = fs.ReadChar();
                    // check for tag
                    if (c == '<')
                    {
                        // if there's useful content in the text node buffer
                        if (strValueTextNodes.Length > 0)
                        {
                            string strTextNodeContent = strValueTextNodes.ToString().Trim(new char[] { '\r', '\n', '\t' });
                            if (strTextNodeContent.Length > 0)
                            {
                                // add the text node to the children
                                tagTextNode.Value = strTextNodeContent;
                                strValueTextNodes.Clear();
                                Children.Add(tagTextNode);
                                // start a new text node
                                tagTextNode            = new XmlTag(this);
                                tagTextNode.IsTextNode = true;
                            }
                        }
                        // if this is the end of the tag
                        if (fs.PeekChar() == '/')
                        {
                            // break out
                            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);
                        // add to text note buffer
                        strValueTextNodes.Append(c);
                    }
                }

                // set value property
                Value = strValue.ToString();
                strValue.Clear();

                // if there's useful content in the text node buffer
                if (strValueTextNodes.Length > 0)
                {
                    string strTextNodeContent = strValueTextNodes.ToString().Trim(new char[] { '\r', '\n', '\t' });
                    if (strTextNodeContent.Length > 0)
                    {
                        // add the text node to the children
                        tagTextNode.Value = strValueTextNodes.ToString();
                        strValueTextNodes.Clear();
                        Children.Add(tagTextNode);
                    }
                }
            }
        }