示例#1
0
        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            string    xml = reader.ReadOuterXml();
            XDocument doc = XDocument.Parse(xml);

            mRoot = RecorderNode.ConvertFrom((XElement)doc.FirstNode, null);
        }
        public bool Contains(string path)
        {
            path = path.Replace("\\", "/");
            int f = path.IndexOf("/");

            if (f == -1)
            {
                string v;
                bool   hasAttr = Attributes.TryGetValue(path, out v);
                if (hasAttr)
                {
                    return(true);
                }
                return(Nodes.Any(o => o.Name == path));
            }

            string       name = path.Substring(0, f);
            RecorderNode node = Nodes.FirstOrDefault(o => o.Name == name);

            if (node == null)
            {
                return(false);
            }
            return(node.Contains(path.Substring(f + 1)));
        }
        public string TryGetValue(string path)
        {
            path = path.Replace("\\", "/");
            int f = path.IndexOf("/");

            if (f == -1)
            {
                string v;
                if (Attributes.TryGetValue(path, out v))
                {
                    return(v);
                }
                return(null);
            }
            string name = path.Substring(0, f);

            RecorderNode node = Nodes.FirstOrDefault(o => o.Name == name);

            if (node == null)
            {
                return(null);
            }

            return(node.TryGetValue(path.Substring(f + 1)));
        }
        public static RecorderNode ConvertFrom(XElement element, RecorderNode parent)
        {
            RecorderNode node = new RecorderNode();

            node.Name       = element.Name.LocalName;
            node.Parent     = parent;
            node.Attributes = element.Attributes().ToDictionary(o => o.Name.LocalName, o => o.Value);
            node.Nodes      = element.Elements().Select(o => ConvertFrom(o, node));
            return(node);
        }
        public RecorderNode TryGetNode(string path)
        {
            path = path.Replace("\\", "/");
            int f = path.IndexOf("/");

            if (f == -1)
            {
                return(Nodes.FirstOrDefault(o => o.Name == path));
            }

            string       name = path.Substring(0, f);
            RecorderNode node = Nodes.FirstOrDefault(o => o.Name == name);

            if (node == null)
            {
                return(null);
            }
            return(node.TryGetNode(path.Substring(f + 1)));
        }
        public IEnumerable <RecorderNode> TryGetNodes(string path)
        {
            path = path.Replace("\\", "/");
            int f = path.IndexOf("/");

            if (f == -1)
            {
                return(Nodes.Where(o => o.Name == path));
            }

            string       name = path.Substring(0, f);
            RecorderNode node = Nodes.FirstOrDefault(o => o.Name == name);

            if (node == null)
            {
                return(new List <RecorderNode>());
            }
            return(node.TryGetNodes(path.Substring(f + 1)));
        }