Esempio n. 1
0
        public override bool TryGetMember(GetMemberBinder binder, out object result) //binder -> property
        {
            result = null;
            bool success = false;

            XElement firstDescendant = Element.Descendants(binder.Name).FirstOrDefault();

            if (firstDescendant != null)
            {
                if (firstDescendant.Descendants().Count() > 0)
                {
                    result = new DynamicXml(firstDescendant);
                }
                else
                {
                    result = firstDescendant.Value;
                }
                success = true;
            }
            return(success);
        }
Esempio n. 2
0
        private static void DynamicXmlExample()
        {
            //Normally
            XElement person = XElement.Parse(
                @"<Person>
                   <FirstName>YAO</FirstName>
                   <LastName>CUI</LastName>
                  </Person>");

            Console.WriteLine("{0}  {1}", person.Descendants("FirstName").FirstOrDefault().Value, person.Descendants("LastName").FirstOrDefault().Value);

            dynamic person0 = DynamicXml.Parse(
                @"<Person>
                   <FirstName>YAO</FirstName>
                   <LastName>
                        <FirstChar>C</FirstChar>
                        <SecondChar>U</SecondChar>
                        <ThirdChar>I</ThirdChar>
                    </LastName>
                  </Person>");

            Console.WriteLine("{0}  {1}", person0.FirstName, person0.LastName.FirstChar + person0.LastName.SecondChar + person0.LastName.ThirdChar);
        }