February, 2007

  • Wriju's BLOG

    XLinq: Create XML from object using LINQ

    • 4 Comments

     

    We have seen how to create the XML content from raw data initializing elements and attributes one by one. More realistic approach in the world of application development would be to read the values either from relational database or collection. Let’s use the combined power of LINQ and XLinq to create XML content easily and smartly.

     

    Using C# 3.0 enhancement, implicitly typed array which is the combination of anonymous object initializers to create the anonymously typed data structures.

    My famous Customers object, defined 

     

    var objCust = new[]

    {

     new {CustID = 2, CustName = "Sumitra", Phone = "123-123-1236"},

     new {CustID = 3, CustName = "Wriju", Phone = "123-123-1235"},

     new {CustID = 4, CustName = "Writam", Phone = "123-123-1234"},

     new {CustID = 1, CustName = "Debajyoti", Phone = "123-123-1237"}   

    };

     

    Now we will create XML out of it with proper order (notice that the CustID is not in proper sequence). We can filter the data with the help of LINQ.

     

    XElement _customers = new XElement("customers",

                            from c in objCust

                            orderby c.CustID //descending 

                            select new XElement("customer",

                                new XElement("name", c.CustName),

                                new XAttribute("ID", c.CustID),

                                new XElement("phone", c.Phone)

                                                )

                                        );

     

    Console.WriteLine(_customers);

     

    The output will look like.

     

    <customers>

      <customer ID="1">

        <name>Debajyoti</name>

        <phone>123-123-1237</phone>

      </customer>

      <customer ID="2">

        <name>Sumitra</name>

        <phone>123-123-1236</phone>

      </customer>

      <customer ID="3">

        <name>Wriju</name>

        <phone>123-123-1235</phone>

      </customer>

      <customer ID="4">

        <name>Writam</name>

        <phone>123-123-1234</phone>

      </customer>

    </customers>

     

    Isn’t it cool!!!! Yes, yes, yes….

     

    Namoskar

     

  • Wriju's BLOG

    XLinq: Beginners Walkthrough

    • 6 Comments

     

    Let’s play with Linq to XML (aka XLinq). XLinq is different from conventional .NET way to work with XML. Linq to XML is not the replacement it is smarter and superior way to work with XML with Language-INtegrated Query.

     

    Let’s suppose you need to create XML content like

     

    <employees>

      <employee age="28">

        <name>Wriju</name>

        <email>wriju@abc.com</email>

      </employee>

      <employee age="18">

        <name>Writam</name>

        <email>writam@abc.com</email>

      </employee>

    </employees>

     

    using the power of XLinq. If you notice my code here you will find the name of objects under XLinq library start with “X” instead of Xml.

     

    Here we go

     

    XElement _xml = new XElement("employees",

                        new XElement("employee",

                            new XElement("name", "Wriju"),

                            new XAttribute("age", "28"),

                            new XElement("email", "wriju@abc.com")

                    ),

                        new XElement("employee",

                            new XElement("name", "Writam"),

                            new XAttribute("age", "18"),

                            new XElement("email", "writam@abc.com")

                        )

                    );

     

    Console.WriteLine(_xml);

     

    You can also save the content in proper XML format rather than just showing them in string.

     

    _xml.Save(@"C:\MyXMLData.xml");

     

    Now this XML has couple of things missing like <?xml version="1.0" encoding="utf-8"?>

     

    To add this you need to create XDocument instead of XElement.

     

    The code will look like

     

    XDocument _doc = new XDocument(

        new XDeclaration("1.0", "utf-8", "yes"),

        new XComment("Sample XML"),

        new XElement("employees",

                        new XElement("employee",

                            new XElement("name", "Wriju"),

                            new XAttribute("age", "28"),

                            new XElement("email", "wriju@abc.com")

                    ),

                        new XElement("employee",

                            new XElement("name", "Writam"),

                            new XAttribute("age", "18"),

                            new XElement("email", "writam@abc.com")

                        )

                    )

    );

     

    To just display the data use

     

    Console.WriteLine(_doc);

     

    To save use,

    _doc.Save(@"C:\MyXML.xml");

     

    Happy weekend!!!

     

    Namoskar

     

  • Wriju's BLOG

    LINQ Resources

    • 0 Comments

     

    Let us compile the resources on LINQ Project and its components. To work with LINQ there are two options available for you. One is to install the Orcas CTP from MSDN and the other is with the Visual Studio 2005 install the LINQ May 2006 CTP. I would prefer to use the second option. Mainly because we are working on Orcas and few features that are available with LINQ May CTP are not available in Orcas January CTP.

     

    Download Microsoft Visual Studio Code Name “Orcas” Language-Integrated Query, May 2006 Community Technology Preview and install on top of Visual Studio 2005. This will have the compiler support for C# 3.0 / VB 9.0 language enhancements along with IntelliSense support.

     

    Once you have it in your machine you would be able to see the templates in File > New Project > C# > LINQ Preview which will contain

    Ø       LINQ Console Applications

    Ø       LINQ Windows Applications

    …..

     

     

    LINQ Project has multiple components like,

    Ø       LINQ to Object (formerly known as LINQ)

    Ø       LINQ to SQL (formerly known as DLinq)

    Ø       LINQ to XML (formerly known as XLinq)

     

    MSDN Articles

    Ø       LINQ FAQ

    Ø       C# 3.0 Language Specification

    Ø       C# 3.0 Language Enhancements Chat

    Ø       The LINQ Project

    Ø       LINQ Project Overview

    Ø       Standard Query Operators

    Ø       DLinq Overview for C# Developers

    Ø       LINQ to Entities

    Ø       DLinq Designer

    Ø       XLinq Overview

    Ø       LINQ Over DataSet for C# Developers

     

    LINQ to Object (LINQ) Resources

    +++++++++++++++++

    We have 101 LINQ Samples on LINQ to Object which I think will give you clearer picture on its capability.

     

    Once you have installed the LINQ Project in your PC you will find Hands-on Lab and samples in the installed folder (e.g., C:\Program Files\LINQ Preview). So please go ahead and explore them.

     

    LINQ to SQL (DLinq) Resources

    ++++++++++++++++++++

     

    There is a nice walkthrough available at MSDN. Please download the instruction from DLinq Designer for VB and C#

     

    For lap-around with the SqlMetal.exe (the command line tool equivalent to DLinq Designer) for LINQ to SQL (DLinq), I have added an entry in my blog titled DLinq: ADO.NET vNext a lap around.

     

    LINQ to XML (XLinq) Resources

    ++++++++++++++++++++

    LINQ to XML is not the replacement to System.Xml namespace. It is smarter and quicker way to handle XML from .NET. LINQ to XML is element centric and consumes less memory. The object starts with “X” not “Xml”. For example in LINQ to XML it is XElement not XmlElement (as for System.Xml).

     

    I have added in my blog entries for LINQ to XML.

     

    Ø       XLinq: Beginners Walkthrough

    Ø       XLinq: Create XML from object using LINQ

     

    Added resources

     

    LINQ on Channel9

    Watch this Channel9 video with Anders Hejlsberg and Luca Bolognese as they explain what The LINQ Project is, and how it will help you create XML and data-driven applications.   

     

    More LINQ on Channel9

    Anders Hejlsberg and Sam Drucker discuss LINQ and LINQ to Entities.

     

    Namoskar

     

  • Wriju's BLOG

    LINQ in not only for Object Query

    • 1 Comments

     

    Hello all,

     

    Here we will explore another side of LINQ (Language-INtegrated Query). We all know that LINQ is to query the object (even to the System object). Using the System.Reflection namespace we can get the list of Methods, Fields etc using LINQ Query (with the filter and sort). Lets see one small example on that,

     

    IEnumerable<Type> all = from t in typeof(SqlConnection).Assembly.GetTypes()

                            where t.Name == "SqlConnection"                                   

                            orderby t.Name

                            select t;

     

    foreach(var v in all)

    {

        textBox1.Text += "Start:Methods of " +  v.Name   + "\r\n";

        textBox1.Text += "===================================\r\n";

        foreach(MemberInfo mi in v.GetMembers())

            textBox1.Text += mi.Name   + "\r\n";

    }

     

    The output will display the list of methods from the SqlConnection class.

     

    Namoskar

     

Page 1 of 1 (4 items)