/// <summary> /// 轉換為 Duplicate 模式。 /// </summary> private void ToDuplicateMode() { if (!UniqueMode) return; UniqueMode = false; XmlObject newobj = new XmlObject(Name()); newobj._string_value = _string_value; newobj._children = _children; newobj._xml_record_dictionary = _xml_record_dictionary; XmlRecordList.Add(newobj); }
internal static XmlObject ParseList(IEnumerable<XElement> xmlList) { XmlObject result = new XmlObject() { UniqueMode = false }; result.SetName(xmlList.First().Name.LocalName); foreach (XElement elm in xmlList) result.XmlRecordList.Add(XmlObject.Parse(elm)); return result; }
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; } }
/// <summary> /// 產生 XData 物件。 /// </summary> /// <param name="xml"></param> /// <returns></returns> public static XmlObject Parse(XElement xml) { dynamic root = new XmlObject(xml.Name.LocalName); foreach (XAttribute att in xml.Attributes()) root["@" + att.Name.LocalName] = att.Value; Dictionary<string, int> nameCount = new Dictionary<string, int>(); foreach (XElement elm in xml.Elements()) { if (nameCount.ContainsKey(elm.Name.LocalName)) nameCount[elm.Name.LocalName]++; else nameCount[elm.Name.LocalName] = 1; } Dictionary<string, bool> processed = new Dictionary<string, bool>(); foreach (XElement elm in xml.Elements()) { string elmName = elm.Name.LocalName; if (nameCount[elmName] > 1) { if (processed.ContainsKey(elmName)) continue; root[elmName] = XmlObject.ParseList(xml.Elements(elmName)); } else { if (elm.HasAttributes || elm.HasElements) root[elmName] = XmlObject.Parse(elm); else root[elmName] = elm.Value; } } return root; }