Convert XML data to dynamic (ExpandoObject) Object

 public static IEnumerable<dynamic> GetExpandoFromXml(string file, string descendantid)  
 {  
   var expandoFromXml = new List<dynamic>();  
   var doc = XDocument.Load(file);  
   var nodes = doc.Root.Descendants(descendantid);  
   foreach (var element in doc.Root.Descendants(descendantid))  
   {  
     dynamic expandoObject = new ExpandoObject();  
     var dictionary = expandoObject as IDictionary<string, object>;  
     foreach (var child in element.Descendants())  
     {  
       if (child.Name.Namespace == "")  
         dictionary[child.Name.ToString()] = child.Value.Trim();  
     }  
     yield return expandoObject;  
   }  
 }  

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2