Пример #1
0
        /// <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);
        }
Пример #2
0
        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);
        }