Monday, February 6, 2012

The type or namespace name 'Data' does not exist in the namespace 'Microsoft.Practices.EnterpriseLibrary' (are you missing an assembly reference?)

Recently i was working on a sample WCF service project. For some database functionality, i installed EnterpriseLibrary and added the reference to the following DLL's in my project:-

i) Microsoft.Practices.EnterpriseLibrary.Data
ii) Microsoft.Practices.EnterpriseLibrary.Common

I tried to use the built in database features of EnterpriseLibrary.Data dll to do some database operation. But on building the project follwing errors were shown:-

a) The type or namespace name 'Data' does not exist in the namespace    'Microsoft.Practices.EnterpriseLibrary' are you missing an assembly reference?)    

b) The type or namespace name 'Database' could not be found (are you missing a using
  directive or an assembly reference?)

It seemed like there was some issue with the EnterpriseLibrary setup installation or DLL reference. After trying some basic stuff's, I uninstalled and installed EnterpriseLibrary multiple times but it didn't work. Tried different ways to refer the EnterpriseLibrary DLL's but that too didn't work out.

After lot of checking i found out that the issue was pretty simple.By default the WCF test project was targeted against the  .NET Framework 4 Client Profile (which was installed on my machine).

The .NET Framework 4 Client Profile target framework for the project was the only reason i was getting this DLL reference error.

[To know about the .Net Framework 4 Client Profile please refer following MSDN link:   
 http://www.microsoft.com/download/en/details.aspx?id=24872]

So i changed the targeted framework to .NET Framework 4 using the project properties.
And finally everything started working fine. A simple change in the project properties but the impact was big.

Hopefully this post can help someone to resolve this small issue and save a lot of debugging time.



Happy Coding.........

Saturday, February 4, 2012

Adding New Element with Attributes in XML document using LINQ

Today's post will show a way to add new element to the existing XML Document with list of attributes name as contained in other elements with the help of LINQ in C#. 

Herein the sample customers.xml file used for the current example

Customers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <customer name="Steve John" Id="CUST0001" location="New York" 
  country="United States" DOB="12/23/1960Phone="+12126661111"/>
  <customer name="Sagar Mehta" Id="CUST0106" location="Bangalore" 
   country="India" DOB="07/23/1978" Phone="+918088881001" />
  <customer name="Rahul Raj" Id="CUST0190" location="Delhi" 
   country="India" DOB="01/31/1980" Phone="+911166661111" />
  <customer name="John Roderix" Id="CUST0301" location="Sydney" 
   country="Australia" DOB="02/13/1956" Phone="+61(0)280623999"/>
</Customers>

//Loading the customers xml into the XDocument object
XDocument xDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory+ "/Customers.xml");


There could be different ways to add the same attributes details as exists in other elements.

Scenario 1: Adding the New element with Attributes details using Attributes name in the XElement creation.

Herein a new element is created with the existing set of attributes name.


XElement oXElementNew = 
       new XElement("Customer"
             new XAttribute("name", "Martin King"),
             new XAttribute("city", "New York"),
             new XAttribute("country", "USA"),
             new XAttribute("emailid", "martinking@martin.com"));

Now newly created element "oXElementNew" will be added to the existing xDoc object of XML document.

xDoc.Root.Add( oXElementNew );

Now all the Element details from the XML document will be retrieved to see whether the newly added Element is added to the xDoc object.

//retreiving all the elements from the xDoc object

IEnumerable<XElement> rows = 
                         from row in xDoc.Descendants("Customer")
                         select row;


//looping through all the elements 
foreach (XElement xe in rows)
{
  //retreiving all the attributes from the current xElement 
  IEnumerable<XAttribute> attList = 
                       from att in            
                       xe.DescendantsAndSelf().Attributes()
                       select att;

 //looping through the attributes collection
 foreach (XAttribute xAtt in attList)
 {
    //displaying the individual attributes details for the 
    //selected element
    Console.WriteLine(xAtt);
 }
 //to display a separator between two customer records
 Console.WriteLine("---Customer details separator---");
}
Output:-

The newly created element details can be seen in the output window.

Scenario 2: First getting the count of Attributes in a element and then creating the New element with Attributes details using Attributes array index position.


//getting the count of Attributes from an Element of XML Document
int attributesCount = xDoc.Descendants("Customer").ElementAt(0).Attributes().Count();

//Definiting an array varible of type XAttribute and defining the array size using the //attributesCount
XAttribute[] xAttribute = new XAttribute[attributesCount];

//assigning the each attributes index a value
xAttribute[0] = new XAttribute("name","Rahul Pradhan");
xAttribute[1] = new XAttribute("city", "Hyderabad");
xAttribute[2] = new XAttribute("country", "India");
xAttribute[3] = new XAttribute("emailid", "rahul.p@gmail.com");

//creating the new xElement by passing the attribute details
XElement oXElementNew = new XElement("Customer", xAttribute);

//adding the newly created element to the Xml root node
xDoc.Root.Add(oXElementNew);

//now we will retrieve all the elements from the xDoc object 
IEnumerable<XElement> rows = 
                         from row in xDoc.Descendants("Customer")
                         select row;


//looping through all the elements 
foreach (XElement xe in rows)
{
  //retreiving all the attributes from the current xElement 
  IEnumerable<XAttribute> attList = 
                       from att in            
                       xe.DescendantsAndSelf().Attributes()
                       select att;

 //looping through the attributes collection
 foreach (XAttribute xAtt in attList)
 {
    //displaying the individual attributes details for the 
    //selected element
    Console.WriteLine(xAtt);
 }
 //to display a separator between two customer records
 Console.WriteLine("---Customer details separator---");
}

Output:-


The newly created element details can be seen in the output window.

I have used Console.Write for writing the output to demonstrate the output retrieved. You can use any way the data is required to be displayed or stored in a collection.


Happy Coding.........