IBM Database Add-Ins for Visual Studio  

Overview: Setting up .NET Applications to Write LINQ to XML Queries on XML Data

With the IBM® Database Add-Ins for Visual Studio, you can add an XML document and its reference to a Visual Studio project. You then can write LINQ to XML queries on the associated XML data, so that you can use the Visual Studio IntelliSense features for XML and LINQ.

For information about writing LINQ to XML queries, refer to .NET Language-Integrated Query for XML Data on the Microsoft Developer Network (MSDN) Web site.

To use this feature, you select an XML source. You can use any of the following objects as the XML source:

If you select an XML schema as the XML source, a sample XML document is generated. If you select an XML document as the XML source, an XML schema is generated for the XML document. Both the XML document file and the XML schema file are copied to the Visual Studio project directory. References to both files are added to the project.

This feature also generates sample code and adds it to the project. The sample code demonstrates how to write a LINQ to XML query for the XML document that was added to the project.

You have the option of generating a class that maps to the XML document. If you select this option, the generated mapping class represents the XML document.

Note:  This feature is available in Visual Studio 2008 or later. However, to use LINQ to XML in Visual Studio 2008, Microsoft .NET Framework 3.5 must be installed. To use LINQ to XML in Visual Studio 2010, Microsoft .NET Framework 4 must be installed.


Example:  The following XML document is stored in the porder column of the first record in the invoice database table.

    <PurchaseOrder xlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   PoNum="PoNum1" OrderDate="1900-01-01" Status="Status1"
                   xmlns="http://posample.org">

        <partid>partid1</partid> 
        <name>name1</name> 
        <quantity>1</quantity> 
        <price>1</price>

    </PurchaseOrder>fs

If you use this document as the XML source, the PurchaseOrder.xml file is created from the XML document. An XML schema is generated for the XML document and stored in the PurchaseOrder.xsd file. Both files are copied to the Visual Studio project directory, and references to both files are added to the project.

If you do not select the option to generate a class to map to the XML source, the following sample LINQ to XML query code is generated and added to the project. The name of the file that contains the sample code is PurchaseOrder, with an extension that reflects the programming language of the project. The code includes the IEnumerable collection porders that is filled with anonymous objects Partid, Name, Quantity, and Price.


    XDocument xdoc = XDocument.Load ("PurchaseOrder.xml");
    var porders = from purchaseOrder in xdoc.Descendants("PurchaseOrder")

        select new
        {
          Partid = purchaseOrder.Element ("partid").Value,
          Name = purchaseOrder.Element ("name").Value,
          Quantity = purchaseOrder.Element ("quantity").Value,
          Price = purchaseOrder.Element ("price").Value,
        }; 
        foreach (var porder in porders) 
        {

            Console.WriteLine (porder.Partid); 
            Console.WriteLine (porder.Name); 
            Console.WriteLine (porder.Quantity); 
            Console.WriteLine (porder.Price);

        }

If you select the option to generate a class to map to the XML source, the following sample code is generated and added to the project. The name of the file that contains the generated class and sample code is PurchaseOrder, with an extension that reflects the programming language of the project. The PurchaseOrder class maps to the XML source document in the project.

    class PurchaseOrder

        { 
          public string Partid { get; set; } 
          public string Name { get; set; } 
          public string Quantity { get; set; } 
          public string Price { get; set; } 
        {

    XDocument xdoc = XDocument.Load ("PurchaseOrder.xml");

        List<PurchaseOrder> purchaseOrder =

            (from pOrder in xdoc.Descendants("PurchaseOrder") 
             select new PurchaseOrder 
             {

                Partid = pOrder.Element("partid").Value, 
                Name = pOrder.Element("name").Value, 
                Quantity = pOrder.Element("quantity").Value, 
                Price = pOrder.Element("price").Value,

             } 
            ).ToList<PurchaseOrder>(); 
            foreach (var porder in purchaseOrder) 
            {

                Console.WriteLine (porder.Partid); 
                Console.WriteLine (porder.Name); 
                Console.WriteLine (porder.Quantity); 
                Console.WriteLine (porder.Price);

            }

See Also

Setting up .NET Applications to Write LINQ to XML Queries on XML Data | Managing XML Data


.NET Development Forum   developerWorks: Visual Studio .NET   DB2 FAQs   IDS FAQs

© Copyright IBM Corporation 2002, 2019. All Rights Reserved.