Monthly Features: Technical Showcase<br />What is LINQ to XML?<br />Microsoft says:<br />“LINQ to XML provides an in-memor...
Monthly Features: Technical Showcase<br />What is LINQ to XML?<br />Alternative to using XPath<br />Uses anonymous types t...
Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders>...
Monthly Features: Technical Showcase<br />Get PurchaseIds of orders greater than $40.00<br />Returns<br />XDocumentpurchas...
Monthly Features: Technical Showcase<br />Add a reference to System.Xml.Linq.<br />Classes<br />XDocument– Class used to i...
Monthly Features: Technical Showcase<br />Methods and Properties<br />XDocument.Root– Returns the root XElement of the XML...
Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders>...
Monthly Features: Technical Showcase<br />Get Purchase Orders greater than $40.00<br />Returns<br />XDocumentpurchaseOrder...
Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders>...
Monthly Features: Technical Showcase<br />Get Purchase Orders with at least 2 items<br />Returns<br />XDocumentpurchaseOrd...
Monthly Features: Technical Showcase<br />InfoPath<br /><my:myFieldsxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<...
Monthly Features: Technical Showcase<br />Querying a repeating table in InfoPath<br />Reference System.Linq (contained in ...
Monthly Features: Technical Showcase<br />Questions?<br />Code Samples available at www.dotnetmafia.com.<br />
Upcoming SlideShare
Loading in...5
×

Introduction to LINQ To XML

2,675

Published on

Slides from an an internal SBTI talk about LINQ to XML

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,675
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
88
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Talk about demo of purchase orders over $40.00
  • Talk about the anonymous type we will be creating.
  • Item Count
  • Item Count
  • Introduction to LINQ To XML

    1. 1. Monthly Features: Technical Showcase<br />What is LINQ to XML?<br />Microsoft says:<br />“LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework. LINQ to XML uses the latest .NET Framework language capabilities and is comparable to an updated, redesigned Document Object Model (DOM) XML programming interface.”<br />
    2. 2. Monthly Features: Technical Showcase<br />What is LINQ to XML?<br />Alternative to using XPath<br />Uses anonymous types to provide strongly typed access to XML elements and attributes.<br />Not a replacement for XSLT<br />
    3. 3. Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders><br />  <PurchaseOrderId="83123"Total="59.97"><br />    <CustomerName>Joe Bob</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="32324"Name="T-Shirt"Color="White"Price="9.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83124"Total="19.99"><br />    <CustomerName>Bob Joe</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Red"Price="19.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83125"Total="49.98"><br />    <CustomerName>John Smith</CustomerName><br />    <CustomerState>OK</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br /></PurchaseOrders><br />
    4. 4. Monthly Features: Technical Showcase<br />Get PurchaseIds of orders greater than $40.00<br />Returns<br />XDocumentpurchaseOrderXml = XDocument.Load("PurchaseOrder.xml");<br /> <br />varpurchaseIds = frompurchaseOrderinpurchaseOrderXml.Root.Elements("PurchaseOrder")<br />                  wherefloat.Parse(purchaseOrder.Attribute("Total").Value) > 40.00f<br />                  selectpurchaseOrder.Attribute("Id").Value;<br /> <br />foreach (varpurchaseIdinpurchaseIds)<br />  Console.WriteLine("PurchaseId: {0}", purchaseId);<br />Order Ids Greater than $40.00<br />PurchaseId: 83123<br />PurchaseId: 83125<br />
    5. 5. Monthly Features: Technical Showcase<br />Add a reference to System.Xml.Linq.<br />Classes<br />XDocument– Class used to interact with an XML document<br />XElement– Represents an element in an XML document<br />XAttribute– Represents an attribute in an XML document<br />XNamespace – Used to specify a namespace in an XML document <br />i.e.: /my:myFields/my:RepeatingTable<br />LINQ to XML Classes<br />
    6. 6. Monthly Features: Technical Showcase<br />Methods and Properties<br />XDocument.Root– Returns the root XElement of the XML document<br />XContainer.Elements() – Returns an IEnumberable<XElement> of all matching elements <br />Example: myDocument.Root.Elements(“PurchaseOrders”);<br />XContainer.Attributes() – Returns an IEnumerable<XAttribute><br />XContainer.Element() – Returns a single XElement<br />Example: purchaseOrderElement.Element(“CustomerName”).Value;<br />XContainer.Attribute() – Returns a single XAttribute<br />.Any() Extension Method – Returns true if items exist in a IEnumerable<><br />.First() Extension Method – Returns first item in an IEnumberable<><br />LINQ to XML Methods and Properties<br />
    7. 7. Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders><br />  <PurchaseOrderId="83123"Total="59.97"><br />    <CustomerName>Joe Bob</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="32324"Name="T-Shirt"Color="White"Price="9.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83124"Total="19.99"><br />    <CustomerName>Bob Joe</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Red"Price="19.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83125"Total="49.98"><br />    <CustomerName>John Smith</CustomerName><br />    <CustomerState>OK</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br /></PurchaseOrders><br />
    8. 8. Monthly Features: Technical Showcase<br />Get Purchase Orders greater than $40.00<br />Returns<br />XDocumentpurchaseOrderXml = XDocument.Load("PurchaseOrder.xml");<br /> <br />varpurchaseOrders = frompurchaseOrderinpurchaseOrderXml.Root.Elements("PurchaseOrder")<br />                     wherefloat.Parse(purchaseOrder.Attribute("Total").Value) > 40.00f<br />                     selectnew<br />                     {<br />                       Id = purchaseOrder.Attribute("Id").Value,<br />                       Total = float.Parse(purchaseOrder.Attribute("Total").Value),<br />                       CustomerName = purchaseOrder.Element("CustomerName").Value,<br />                       CustomerState = purchaseOrder.Element("CustomerState").Value<br />                     };<br /> <br />foreach (varpurchaseOrderinpurchaseOrders)<br />  Console.WriteLine("PurchaseId: {0}: {1} ({2}) - {3}",<br />      purchaseOrder.Id,<br />      purchaseOrder.CustomerName,<br />      purchaseOrder.CustomerState,<br />      purchaseOrder.Total);<br />Returns<br />Orders Greater than $40.00<br />PurchaseId: 83123: Joe Bob (TX) - 59.97<br />PurchaseId: 83125: John Smith (OK) - 49.98<br />
    9. 9. Monthly Features: Technical Showcase<br />Sample XML File<br /><?xmlversion="1.0"encoding="utf-8" ?><br /><PurchaseOrders><br />  <PurchaseOrderId="83123"Total="59.97"><br />    <CustomerName>Joe Bob</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="32324"Name="T-Shirt"Color="White"Price="9.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83124"Total="19.99"><br />    <CustomerName>Bob Joe</CustomerName><br />    <CustomerState>TX</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Red"Price="19.99" /><br />    </Items><br />  </PurchaseOrder><br />  <PurchaseOrderId="83125"Total="49.98"><br />    <CustomerName>John Smith</CustomerName><br />    <CustomerState>OK</CustomerState><br />    <Items><br />      <ItemId="26134"Name="Polo Shirt"Color="Blue"Price="19.99" /><br />      <ItemId="01283"Name="Jeans"Color="Blue"Price="29.99" /><br />    </Items><br />  </PurchaseOrder><br /></PurchaseOrders><br />
    10. 10. Monthly Features: Technical Showcase<br />Get Purchase Orders with at least 2 items<br />Returns<br />XDocumentpurchaseOrderXml = XDocument.Load("PurchaseOrder.xml");<br /> <br />varpurchaseOrders = frompurchaseOrderinpurchaseOrderXml.Root.Elements("PurchaseOrder")<br />                     wherepurchaseOrder.Element("Items").Elements("Item").Count() >= 2<br />                     selectnew<br />                     {<br />                         Id = purchaseOrder.Attribute("Id").Value,<br />                         ItemCount = purchaseOrder.Element("Items").Elements("Item").Count()<br />                     };<br /> <br />foreach (varpurchaseOrderinpurchaseOrders)<br />    Console.WriteLine("PurchaseId: {0}: ItemCount: {1}",<br />        purchaseOrder.Id,<br />        purchaseOrder.ItemCount);<br />Returns<br />Orders With at least 2 items<br />PurchaseId: 83123: ItemCount: 3<br />PurchaseId: 83125: ItemCount: 2<br />
    11. 11. Monthly Features: Technical Showcase<br />InfoPath<br /><my:myFieldsxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />             xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37"<br />             xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"xml:lang="en-US"><br />  <my:RepeatingTable><br />    <my:Item><br />      <my:Name>Polo Shirt</my:Name><br />      <my:Color>Red</my:Color><br />      <my:Price>19.99</my:Price><br />    </my:Item><br />    <my:Item><br />      <my:Name>Jeans</my:Name><br />      <my:Color>Blue</my:Color><br />      <my:Price>29.99</my:Price><br />    </my:Item><br />  </my:RepeatingTable><br />  <my:Id>83123</my:Id><br /></my:myFields><br />
    12. 12. Monthly Features: Technical Showcase<br />Querying a repeating table in InfoPath<br />Reference System.Linq (contained in System.Core)<br />Reference System.Xml.Linq (contained in System.Xml.Linq)<br />Returns<br />// use the MainDataSource to read the XML of the InfoPath Form<br />XDocumentinfoPathDocument = XDocument.Load(newSystem.IO.StringReader(MainDataSource.CreateNavigator().OuterXml));<br /> <br />// required to access my: namespace<br />XNamespacemyNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37";<br /> <br />// use descendants node to find any element with the name Item in the tree<br />var items = from item ininfoPathDocument.Descendants(myNamespace + "Item")<br />            selectnew<br />            {<br />                Name = item.Element(myNamespace + "Name").Value,<br />                Color = item.Element(myNamespace + “Color").Value,<br />                Price = item.Element(myNamespace + "Price").Value<br />            };<br /> <br />foreach (var item in items)<br />{<br />    // do something<br />}<br />
    13. 13. Monthly Features: Technical Showcase<br />Questions?<br />Code Samples available at www.dotnetmafia.com.<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×