コード例 #1
0
ファイル: XmlParser.cs プロジェクト: mbolt35/OpenSource
        /// <summary>
        /// This method parses xml using a <c>StringReader</c> instance and returns an <c>Xml</c> instance
        /// containing the parsed data.
        /// </summary>
        /// <param name="stringReader">The <c>StringReader</c> instance containing the xml data to parse</param>
        /// <returns>An <c>Xml</c> instance containing the parsed data.</returns>
        public Xml Parse(StringReader stringReader)
        {
            using (XmlReader reader = XmlReader.Create(stringReader, _settings)) {
                Xml currentElement = null;
                Stack<Xml> elementStack = new Stack<Xml>();

                while (reader.Read()) {

                    switch (reader.NodeType) {

                        // Handle Open Element, <element>
                        case XmlNodeType.Element:
                            Xml xmlElement = new Xml(reader.Name, reader.NamespaceURI);

                            // Check for Attributes and Read All Attributes
                            if (reader.HasAttributes) {
                                while (reader.MoveToNextAttribute()) {
                                    xmlElement.AddAttribute(reader.Name, reader.Value);
                                }

                                // Move back to the element
                                reader.MoveToElement();
                            }

                            // Handles node that only contains attributes: <someNode x='10' y='5'/>
                            if (!reader.HasValue && reader.IsEmptyElement) {
                                currentElement.AppendElement(xmlElement);
                                break;
                            }

                            elementStack.Push(xmlElement);

                            if (currentElement == null) {
                                currentElement = xmlElement;
                            } else {
                                currentElement.AppendElement(xmlElement);
                                currentElement = xmlElement;
                            }

                            break;

                        // Handle attribute or element text, ... prop="myProp" ...
                        case XmlNodeType.Text:
                            currentElement.Value = reader.Value;
                            break;

                        // Handle end element, </element>
                        case XmlNodeType.EndElement:
                            if (elementStack.Count > 1) {
                                elementStack.Pop();

                                currentElement = elementStack.Peek();
                            } else if (elementStack.Count > 0) {
                                elementStack.Pop();
                            }

                            break;
                    }
                }

                return currentElement;
            }
        }
コード例 #2
0
ファイル: Xml.cs プロジェクト: mbolt35/OpenSource
        /// <summary>
        /// This method creates a cloned copy of this instance and returns it.
        /// </summary>
        public Xml Clone()
        {
            if (_isUndefined) {
                throw new Exception("Cannot clone the undefined Xml node.");
            }

            Xml clone = new Xml(_name, _namespaceUri);

            foreach (string key in _attributes.Keys) {
                clone.AddAttribute(key, _attributes[key]);
            }

            foreach (Xml element in Elements) {
                clone.AppendElement(element);
            }

            clone.Value = Value;

            return clone;
        }