August 22, 2019

C# - How to create List of Anonymous Type objects

Many times you need to create a temporary list of anonymous type objects. For small tasks, like binding a list to a DropDownList control. At that moment you don't want to define a separate class just to keep two properties (i.e. Id & Name) and bind it to the DropDownList's DataSource.

There could be numerous ways to define list of anonymous types and all of these need to infer type from somewhere. I will write some of them here which you can use to create generic list.

  1. First create the object(s) of anonymous type and then pass it to an array and call ToList() method.

       var o1 = new { Id = 1, Name = "Foo" };
       var o2 = new { Id = 2, Name = "Bar" };
    
       var list = new[] { o1, o2 }.ToList();
      
  2. Define a list of dynamic and then populate with anonymous objects.

       List<dynamic> list = new List<dynamic>();
       var o1 = new { Id = 1, Name = "Foo" };
       var o2 = new { Id = 2, Name = "Bar" };
    
       list.Add(01);
       list.Add(02);
      
  3. Define a generic method accepting params T[], and return new List<T>. Pass objects of anonymous type to this method and it will create the List<T>.

       //Define method
       public static List<T> CreateList<T>(params T[] items)
       {
         return new List<T>(items);
       }
    
       //create anonymous objects and call above method
       var o1 = new { Id = 1, Name = "Foo" };
       var o2 = new { Id = 2, Name = "Bar" };
       var list = CreateList(o1, o2);
      
  4. You can use Select() method on Enumerable.Range() to project anonymous objects and then call ToList() method. Once empty list is created, then start adding objects.

       var list = Enumerable.Range(0, 0).Select(e => new { Id = 0, Name = ""}).ToList();
       list.Add(new { Id = 1, Name = "Foo" } );
       list.Add(new { Id = 2, Name = "Bar" } );
      
  5. Enumerable.Repeat() method can also do the trick.

       var list = Enumerable.Repeat(new { Id = 0, Name = "" }, 0).ToList();
       list.Add(new { Id = 1, Name = "Foo" } );
       list.Add(new { Id = 2, Name = "Bar" } );
      
  6. Select() projection method can also use on simple string.

       var list = "".Select( t => new {Id = 0, Name = ""} ).ToList();
       list.Add(new { Id = 1, Name = "Foo" } );
       list.Add(new { Id = 2, Name = "Bar" } );
      

August 19, 2019

Select XML Nodes with XML Namespaces from an XmlDocument and XDocument

To select XML Nodes with Namespace we have to use type XmlNamespaceManager. The AddNamespace() method of XmlNamespaceManager object takes two arguments: one is prefix and the second is the Uri.

Let's say we have following xml:

 <md:ProductOutput xmlns:cb="http://mydomain.com/report/">
   <md:Header>
  <md:Child1 SubjectStatus="">
   Some text for Child1
  </md:Child1>     
   </md:Header>
   <md:Body DocumentID="356d6d32-5755-4ffb-a8b8-8932577526dd">
  <md:Child2>
   Some test for Child2
  </md:Child2>
   </md:Body>
 </md:ProductOutput>

Using XmlDocument:

When selecting nodes from XmlDocument object, you have to use an instance of XmlNamespaceManager class. To select the node md:Header, we will pass the XmlNamespaceManager's instance to SelectSingleNode method of XmlDocument.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlString); // xmlString variable contains above xml

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("md", "http://mydomain.com/report/");

XmlNode nodeHeader = xmlDocument.SelectSingleNode("//md:Header", nsmgr);
//nodeHeader contains the required node

Using XDocument:

To select the node elemment from XDoocument, we have to concatenate node name (Header) with the XNamespace and pass this to Element() method of XDocument.Root Element.

var xdoc = XDocument.Parse(xmlString); // xmlString variable contains above xml
XNamespace ns = "http://mydomain.com/report/";

var elementHeader = xdoc.Root.Element(ns + "Header");
//elementHeader contains the required element