示例#1
0
 internal XNode(XFile owner, string tag)
 {
     _parent = null;
     Owner = owner;
     Tag = tag;
     Nodes = new List<XNode>();
 }
示例#2
0
 public XNode(XNode parent, string tag)
 {
     _parent = parent;
     Owner = parent.Owner;
     Tag = tag;
     Nodes = new List<XNode>();
 }
        public static object Deserialize(XNode node)
        {
            string type = node.Attribute("type");

            switch(type)
            {
                case "string":
                    return node.Attribute("value");

                case "int":
                    return int.Parse(node.Attribute("value"));

                case "float":
                    {
                        var value = node.Attribute("value");

                        if (value == "max")
                        {
                            return float.MaxValue;
                        }

                        if (value == "min")
                        {
                            return float.MinValue;
                        }

                        return float.Parse(value, CultureInfo.InvariantCulture);
                    }

                case "double":
                    {
                        var value = node.Attribute("value");

                        if (value == "max")
                        {
                            return double.MaxValue;
                        }

                        if (value == "min")
                        {
                            return double.MinValue;
                        }

                        return double.Parse(value, CultureInfo.InvariantCulture);
                    }

                case "long":
                    return long.Parse(node.Attribute("value"));

                case "bool":
                    return bool.Parse(node.Attribute("value"));

                case "dateTime":
                    return DateTime.Parse(node.Attribute("value"), CultureInfo.InvariantCulture);
            }

            return null;
        }
示例#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;
        }
        public static bool DeserializeEnum(XNode node, Type enumType, out object value)
        {
            string type = node.Attribute("type");

            if(type == "enum")
            {
                try
                {
                    value = Enum.Parse(enumType, node.Attribute("value"));
                    return true;
                }
                catch
                {}
            }

            value = null;

            return false;
        }
示例#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
        public XNode EnucleateAttributes(string attribPrefix)
        {
            var node = new XNode(_parent, Owner, LineNumber);
            int prefixLength = attribPrefix.Length;

            foreach(var attrib in _attributes)
            {
                if ( attrib.Key.StartsWith(attribPrefix))
                {
                    node._attributes.Add(attrib.Key.Substring(prefixLength), attrib.Value);
                }
            }

            return node;
        }
示例#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;
                    }
                }
            }
        }
示例#10
0
        public static bool Serialize(XNode node, object value)
        {
            if(value is Enum)
            {
                node.AddAttribute("type", "enum");
                node.AddAttribute("value", Enum.GetName(value.GetType(), value));
            }
            else if (value is string)
            {
                node.AddAttribute("type", "string");
                node.AddAttribute("value", (string)value);
            }
            else if (value is int)
            {
                node.AddAttribute("type", "int");
                node.AddAttribute("value", value.ToString());
            }
            else if (value is float)
            {
                float val = (float)value;

                node.AddAttribute("type", "float");

                string valueText = null;

                if(val >= float.MaxValue)
                {
                    valueText = "max";
                }
                else if (val <= float.MinValue)
                {
                    valueText = "min";
                }
                else
                {
                    valueText = string.Format(CultureInfo.InvariantCulture, "{0}", value);
                }

                node.AddAttribute("value", valueText);
            }
            else if (value is double)
            {
                double val = (double)value;

                node.AddAttribute("type", "double");

                string valueText = null;

                if (val >= double.MaxValue)
                {
                    valueText = "max";
                }
                else if (val <= double.MinValue)
                {
                    valueText = "min";
                }
                else
                {
                    valueText = string.Format(CultureInfo.InvariantCulture, "{0}", value);
                }

                node.AddAttribute("value", valueText);

            }
            else if (value is long)
            {
                node.AddAttribute("type", "long");
                node.AddAttribute("value", value.ToString());
            }
            else if (value is bool)
            {
                node.AddAttribute("type", "bool");
                node.AddAttribute("value", value.ToString());
            }
            else if (value is DateTime)
            {
                node.AddAttribute("type", "DateTime");
                node.AddAttribute("value", string.Format(CultureInfo.InvariantCulture, "{0}", value));
            }
            else
            {
                return false;
            }

            return true;
        }