コード例 #1
0
 /// <summary>
 /// 建立。
 /// </summary>
 /// <param name="name">Element 名稱。</param>
 public XmlObject(string name)
     : this()
 {
     _name = new MemberName(name);
 }
コード例 #2
0
        private void SetValue(MemberName name, object value)
        {
            if (_name.IsAttribute)
                throw new ArgumentException("這個節點只允許文字資料。");

            if (value == null)
            {
                if (XmlRecordDictionary.ContainsKey(name))
                    XmlRecordDictionary.Remove(name);

                if (ChildrenDictionary.ContainsKey(name))
                    ChildrenDictionary.Remove(name);

                return;
            }

            if (value is XmlObject)
            {
                XmlObject dl = value as XmlObject;
                dl.SetName(name.FullName);
                XmlRecordDictionary[name] = dl;
            }
            else
            {
                XmlObject newValue = null;

                if (value is XmlObject)
                {
                    newValue = value as XmlObject;

                    if (name.IsAttribute && !newValue._name.IsAttribute)
                        throw new ArgumentException("這個節點只允許文字資料。");

                    //防止自行建立的 XData.Name 與 MemberName 不相同。
                    //因為是否為 Xml 屬性的資訊在 Name 之中。
                    newValue._name = name;
                }
                else
                    newValue = new XmlObject(name.FullName) { _string_value = value + "" };

                ChildrenDictionary[name] = newValue;
            }
        }
コード例 #3
0
        /// <summary>
        /// 將資料轉換成 XLinq 格式。
        /// </summary>
        /// <returns></returns>
        private void Write(XmlWriter writer)
        {
            if (IsLeaf)
            {
                MemberName name = new MemberName(Name());

                if (name.IsAttribute)
                    writer.WriteAttributeString(name.Name, _string_value);
                else
                    writer.WriteElementString(name.Name, _string_value);
            }
            else
            {
                writer.WriteStartElement(Name() == null ? "_UndefineName" : Name());
                foreach (KeyValuePair<MemberName, XmlObject> each in ChildrenDictionary)
                {
                    if (each.Value.UniqueMode)
                        each.Value.Write(writer);
                    else
                    {
                        foreach (XmlObject record in each.Value.XmlRecordList)
                            record.Write(writer);
                    }
                }
                foreach (KeyValuePair<MemberName, XmlObject> xeach in XmlRecordDictionary)
                {
                    foreach (XmlObject each in xeach.Value.Each())
                        each.Write(writer);
                }
                writer.WriteEndElement();
            }
        }
コード例 #4
0
        private XmlObject GetValues(MemberName name)
        {
            if (_xml_record_dictionary == null)
                _xml_record_dictionary = new Dictionary<MemberName, XmlObject>();

            if (!_xml_record_dictionary.ContainsKey(name))
                SetValue(name, new XmlObject());

            return _xml_record_dictionary[name];
        }
コード例 #5
0
 private void SetName(string name)
 {
     _name = new MemberName(name);
 }
コード例 #6
0
        private XmlObject GetValue(MemberName name)
        {
            if (_children == null)
                _children = new Dictionary<MemberName, XmlObject>();

            if (!_children.ContainsKey(name))
                SetValue(name, string.Empty);

            return _children[name];
        }
コード例 #7
0
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            MemberName mn = new MemberName(binder.Name);

            if (UniqueMode)
                SetValue(mn, value);
            else
                (EnsureOneAndReturnLast() as dynamic)[mn.FullName] = value;

            return true;
        }
コード例 #8
0
        public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
        {
            MemberName mn = new MemberName(indexes[0] + "");

            if (UniqueMode)
                SetValue(mn, value);
            else
                (EnsureOneAndReturnLast() as dynamic)[mn.FullName] = value;

            return true;
        }
コード例 #9
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            MemberName mn = new MemberName(binder.Name);

            if (UniqueMode)
            {
                if (XmlRecordDictionary.ContainsKey(mn))
                    result = GetValues(mn);
                else
                    result = GetValue(mn);
            }
            else
                result = (EnsureOneAndReturnLast() as dynamic)[mn.FullName];

            return true;
        }
コード例 #10
0
 private XmlObject()
 {
     UniqueMode = true;
     _name = MemberName.Empty;
 }