System.Xml.Schema.Parser.LoadElementNode C# (CSharp) Method

LoadElementNode() private method

private LoadElementNode ( bool root ) : XmlElement
root bool
return System.Xml.XmlElement
        private XmlElement LoadElementNode(bool root)
        {
            Debug.Assert(_reader.NodeType == XmlNodeType.Element);

            XmlReader r = _reader;
            bool fEmptyElement = r.IsEmptyElement;

            XmlElement element = _dummyDocument.CreateElement(r.Prefix, r.LocalName, r.NamespaceURI);
            element.IsEmpty = fEmptyElement;

            if (root)
            {
                _parentNode = element;
            }
            else
            {
                XmlAttributeCollection attributes = element.Attributes;
                if (r.MoveToFirstAttribute())
                {
                    do
                    {
                        if (Ref.Equal(r.NamespaceURI, _schemaNames.NsXmlNs))
                        { //Namespace Attribute
                            _annotationNSManager.AddNamespace(r.Prefix.Length == 0 ? string.Empty : _reader.LocalName, _reader.Value);
                        }
                        XmlAttribute attr = LoadAttributeNode();
                        attributes.Append(attr);
                    } while (r.MoveToNextAttribute());
                }
                r.MoveToElement();
                string ns = _annotationNSManager.LookupNamespace(r.Prefix);
                if (ns == null)
                {
                    XmlAttribute attr = CreateXmlNsAttribute(r.Prefix, _namespaceManager.LookupNamespace(r.Prefix));
                    attributes.Append(attr);
                }
                else if (ns.Length == 0)
                { //string.Empty prefix is mapped to string.Empty NS by default
                    string elemNS = _namespaceManager.LookupNamespace(r.Prefix);
                    if (elemNS != string.Empty)
                    {
                        XmlAttribute attr = CreateXmlNsAttribute(r.Prefix, elemNS);
                        attributes.Append(attr);
                    }
                }

                while (r.MoveToNextAttribute())
                {
                    if (r.Prefix.Length != 0)
                    {
                        string attNS = _annotationNSManager.LookupNamespace(r.Prefix);
                        if (attNS == null)
                        {
                            XmlAttribute attr = CreateXmlNsAttribute(r.Prefix, _namespaceManager.LookupNamespace(r.Prefix));
                            attributes.Append(attr);
                        }
                    }
                }
                r.MoveToElement();

                _parentNode.AppendChild(element);
                if (!r.IsEmptyElement)
                {
                    _parentNode = element;
                }
            }
            return element;
        }