示例#1
0
 public XNode(XNode parent, string tag)
 {
     _parent = parent;
     Owner = parent.Owner;
     Tag = tag;
     Nodes = new List<XNode>();
 }
示例#2
0
 internal XNode(XFile owner, string tag)
 {
     _parent = null;
     Owner = owner;
     Tag = tag;
     Nodes = new List<XNode>();
 }
示例#3
0
        public static XFile Create(string name)
        {
            var xfile = new XFile(name);
            xfile._node = new XNode(xfile, string.Empty);

            return xfile;
        }
示例#4
0
        public static XFile LoadBinary(Stream stream, string name)
        {
            var reader = new BinaryReader(stream);
            var xfile = new XFile(name);

            var node = new XNode(null, xfile, reader);
            xfile._node = node;

            return xfile;
        }
示例#5
0
        public static XFile Create(Stream stream, string name)
        {
            XmlReader reader = XmlReader.Create(stream);

            var xfile = new XFile(name);
            var node = XNode.ReadXml(reader, xfile);

            xfile._node = node;

            return xfile;
        }
示例#6
0
        internal XNode(XNode parent, XFile owner, BinaryReader reader)
        {
            _parent = parent;
            Owner = owner;

            Nodes = new List<XNode>();

            Tag = reader.ReadString();

            int attrCount = reader.ReadInt32();

            for (int idx = 0; idx < attrCount; ++idx)
            {
                string key = reader.ReadString();
                string value = reader.ReadString();

                _attributes.Add(key, value);
            }

            int nodesCount = reader.ReadInt32();

            for (int idx = 0; idx < nodesCount; ++idx)
            {
                var node = new XNode(this, owner, reader);
                Nodes.Add(node);
            }

            if (nodesCount == 0)
            {
                byte contentType = reader.ReadByte();

                if (contentType == ContentTypeString)
                {
                    Value = reader.ReadString();
                }
                else if(contentType == ContentTypeByteArray)
                {
                    int count = reader.ReadInt32();
                    BinaryValue = reader.ReadBytes(count);
                }
            }
        }
示例#7
0
 private XNode(XNode parent, XFile file, int lineNumber)
 {
     _parent = parent;
     Owner = file;
     LineNumber = lineNumber;
     Nodes = new List<XNode>();
 }
示例#8
0
 internal static XNode ReadXml(XmlReader reader, XFile owner)
 {
     return new XNode(reader, owner, null, new Dictionary<string,string>());
 }
示例#9
0
        private XNode(XmlReader reader, XFile owner, XNode parent, Dictionary<string, string> namespaces)
        {
            namespaces = new Dictionary<string, string>(namespaces);

            Owner = owner;
            _parent = parent;

            while (reader.NodeType != XmlNodeType.Element)
            {
                if (reader.EOF || reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }

                    Tag = null;
                    return;
                }
                reader.Read();
            }

            Tag = reader.LocalName;
            Namespace = reader.NamespaceURI;

            IXmlLineInfo xmlInfo = reader as IXmlLineInfo;
            if (xmlInfo != null)
            {
                LineNumber = xmlInfo.LineNumber;
            }
            else
            {
                LineNumber = 0;
            }

            Boolean hasChildren = !reader.IsEmptyElement;
            bool hasBinaryContent = false;
            if (reader.HasAttributes)
            {
                // Read all attributes and add them to dictionary.
                while (reader.MoveToNextAttribute())
                {
                    string key = reader.Name;

                    if (key.StartsWith("xmlns:"))
                    {
                        key = key.Substring(6);
                        namespaces.Add(key, reader.Value);
                    }
                    else if ( key != "xmlns")
                    {
                        if ( key.Contains(":"))
                        {
                            string[] vals = key.Split(':');

                            key = String.Format("{0}:{1}", namespaces[vals[0]], vals[1]);
                        }

                        if (key == "___XNodeExtensionAttribute_HasBinaryContent")
                        {
                            hasBinaryContent = reader.Value == "true";
                        }
                        else
                        {
                            _attributes.Add(key, reader.Value);
                        }
                    }
                }
            }
            else
            {
                reader.Read();
            }

            Nodes = new List<XNode>();

            while (reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.Text)
            {
                if (reader.EOF || reader.NodeType == XmlNodeType.EndElement)
                {
                    return;
                }
                reader.Read();
            }

            if (reader.NodeType == XmlNodeType.Text)
            {
                if (hasBinaryContent)
                {
                    BinaryValue = Convert.FromBase64String(reader.Value);
                }
                else
                {
                    Value = reader.Value;
                }
            }

            if (hasChildren)
            {
                while (true)
                {
                    XNode node = new XNode(reader, owner, this, namespaces);

                    if (node.Tag != null)
                    {
                        Nodes.Add(node);
                    }
                    else
                    {
                        break;
                    }

                    if (reader.EOF)
                    {
                        return;
                    }
                }
            }
        }