Exemple #1
0
        private static XMLNode SecondLoadNode(StreamReader input)
        {
            string rootName = input.ReadLine();

            var root = new XMLNode(rootName.Substring(1, rootName.Length - 2), new Dictionary <string, string>());

            for (string line; (line = input.ReadLine()) != string.Empty && Lstrip(ref line)[1] != '/';)
            {
                var(nodeName, attrs) = Split(Lstrip(ref line), " ");
                attrs = Split(attrs, ">").Item1;
                var nodeAttrs = new Dictionary <string, string>();
                while (attrs != string.Empty)
                {
                    var(head, tail)  = Split(attrs, "\" ");
                    var(name, value) = Split(head, "=");
                    if (name != string.Empty && value != string.Empty)
                    {
                        nodeAttrs[Unquote(name)] = Unquote(value);
                    }
                    attrs = tail;
                }

                root.AddChild(new XMLNode(nodeName.Substring(1), nodeAttrs));
            }
            return(root);
        }
Exemple #2
0
        private static XMLNode FirstLoadNode(StreamReader input, string rootName = "")
        {
            if (rootName == string.Empty)
            {
                rootName = input.ReadLine();
            }

            var root      = new XMLNode(rootName.Substring(1, rootName.Length - 2), new Dictionary <string, string>());
            var nodeAttrs = root.GetDictionary();

            for (string line; (line = input.ReadLine()) != string.Empty && Lstrip(ref line)[1] != '/';)
            {
                if (line == string.Concat("</", rootName.Substring(1)))
                {
                    break;
                }
                if (line == Split(line, "</").Item1)
                {
                    root.AddChild(FirstLoadNode(input, line));
                    continue;
                }
                var(name, closeAction) = Split(Lstrip(ref line), "</");
                var(nodeName, value)   = Split(name, ">");
                nodeName = nodeName.Substring(1, nodeName.Length - 1);
                if (nodeName != closeAction.Substring(0, closeAction.LastIndexOf('>')))
                {
                    throw new ArgumentException("Tag is not opened/closed:"
                                                + nodeName + "!=" + closeAction.Substring(0, closeAction.Length - 2));
                }
                if (name != string.Empty && value != string.Empty)
                {
                    nodeAttrs[nodeName] = value;
                }
            }
            return(root);
        }