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

Contenu connexe

Similaire à Introduction to LINQ To XML

Krazykoder struts2 ui_tags
Krazykoder struts2 ui_tagsKrazykoder struts2 ui_tags
Krazykoder struts2 ui_tags
Krazy Koder
 
NEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator PresentationNEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator Presentation
askankit
 

Similaire à Introduction to LINQ To XML (20)

Struts2
Struts2Struts2
Struts2
 
Relational data as_xml
Relational data as_xmlRelational data as_xml
Relational data as_xml
 
Mark Logic StrangeLoop 2010
Mark Logic StrangeLoop 2010Mark Logic StrangeLoop 2010
Mark Logic StrangeLoop 2010
 
Krazykoder struts2 ui_tags
Krazykoder struts2 ui_tagsKrazykoder struts2 ui_tags
Krazykoder struts2 ui_tags
 
HTML5 Fundamentals
HTML5 FundamentalsHTML5 Fundamentals
HTML5 Fundamentals
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
Digital + Container List
Digital + Container ListDigital + Container List
Digital + Container List
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Xml
XmlXml
Xml
 
Everything You Always Wanted To Know About XML But Were Afraid To Ask
Everything You Always Wanted To Know About XML But Were Afraid To AskEverything You Always Wanted To Know About XML But Were Afraid To Ask
Everything You Always Wanted To Know About XML But Were Afraid To Ask
 
crystal xcelsius and web services by dashboardcafe.com
crystal xcelsius and web services by dashboardcafe.comcrystal xcelsius and web services by dashboardcafe.com
crystal xcelsius and web services by dashboardcafe.com
 
Rendering The Fat
Rendering The FatRendering The Fat
Rendering The Fat
 
Csphtp1 18
Csphtp1 18Csphtp1 18
Csphtp1 18
 
AdvancedXPath
AdvancedXPathAdvancedXPath
AdvancedXPath
 
What is xml
What is xmlWhat is xml
What is xml
 
NEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator PresentationNEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator Presentation
 
Html5 Overview
Html5 OverviewHtml5 Overview
Html5 Overview
 
IBM Lotus Notes Domino XPages and XPages for Mobile
IBM Lotus Notes Domino XPages and XPages for MobileIBM Lotus Notes Domino XPages and XPages for Mobile
IBM Lotus Notes Domino XPages and XPages for Mobile
 
Hyper Text Markup Language - Presentation.pptx
Hyper Text Markup Language - Presentation.pptxHyper Text Markup Language - Presentation.pptx
Hyper Text Markup Language - Presentation.pptx
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 

Plus de Corey Roth

Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
Corey Roth
 

Plus de Corey Roth (20)

Introduction to Microsoft Teams and Office 365 Groups
Introduction to Microsoft Teams and Office 365 GroupsIntroduction to Microsoft Teams and Office 365 Groups
Introduction to Microsoft Teams and Office 365 Groups
 
Compliance and eDiscovery with Office 365
Compliance and eDiscovery with Office 365 Compliance and eDiscovery with Office 365
Compliance and eDiscovery with Office 365
 
Surfacing Your External Data using BCS in SharePoint 2013 - Dev Connections 2013
Surfacing Your External Data using BCS in SharePoint 2013 - Dev Connections 2013Surfacing Your External Data using BCS in SharePoint 2013 - Dev Connections 2013
Surfacing Your External Data using BCS in SharePoint 2013 - Dev Connections 2013
 
Office 365 - Introduction to SharePoint Online Development - SharePoint Conne...
Office 365 - Introduction to SharePoint Online Development - SharePoint Conne...Office 365 - Introduction to SharePoint Online Development - SharePoint Conne...
Office 365 - Introduction to SharePoint Online Development - SharePoint Conne...
 
Configuring SharePoint Search for an Optimal Document Management Experience
Configuring SharePoint Search for an Optimal Document Management ExperienceConfiguring SharePoint Search for an Optimal Document Management Experience
Configuring SharePoint Search for an Optimal Document Management Experience
 
Fives ways to query SharePoint 2013 Search - SharePoint Summit Toronto 2013
Fives ways to query SharePoint 2013 Search - SharePoint Summit Toronto 2013Fives ways to query SharePoint 2013 Search - SharePoint Summit Toronto 2013
Fives ways to query SharePoint 2013 Search - SharePoint Summit Toronto 2013
 
Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
Publishing SharePoint 2013 Apps to the Office Store - Austin SharePoint Users...
 
What you need to know about Search in SharePoint 2013 Preview - DFW SharePoin...
What you need to know about Search in SharePoint 2013 Preview - DFW SharePoin...What you need to know about Search in SharePoint 2013 Preview - DFW SharePoin...
What you need to know about Search in SharePoint 2013 Preview - DFW SharePoin...
 
Office 365 - Introduction to SharePoint Online Development - Lync and Learn
Office 365 - Introduction to SharePoint Online Development - Lync and LearnOffice 365 - Introduction to SharePoint Online Development - Lync and Learn
Office 365 - Introduction to SharePoint Online Development - Lync and Learn
 
New SharePoint development features using Visual Studio 2012 - SharePoint Sat...
New SharePoint development features using Visual Studio 2012 - SharePoint Sat...New SharePoint development features using Visual Studio 2012 - SharePoint Sat...
New SharePoint development features using Visual Studio 2012 - SharePoint Sat...
 
Pitching Office 365 to your Energy Customers - Microsoft Worldwide Partner Co...
Pitching Office 365 to your Energy Customers - Microsoft Worldwide Partner Co...Pitching Office 365 to your Energy Customers - Microsoft Worldwide Partner Co...
Pitching Office 365 to your Energy Customers - Microsoft Worldwide Partner Co...
 
Making the Most of Search in SharePoint Online - TechEd North America
Making the Most of Search in SharePoint Online - TechEd North AmericaMaking the Most of Search in SharePoint Online - TechEd North America
Making the Most of Search in SharePoint Online - TechEd North America
 
New SharePoint development features using Visual Studio 11 - San Antonio Shar...
New SharePoint development features using Visual Studio 11 - San Antonio Shar...New SharePoint development features using Visual Studio 11 - San Antonio Shar...
New SharePoint development features using Visual Studio 11 - San Antonio Shar...
 
Office 365 - Introduction to SharePoint Online Development - SharePoint Satur...
Office 365 - Introduction to SharePoint Online Development - SharePoint Satur...Office 365 - Introduction to SharePoint Online Development - SharePoint Satur...
Office 365 - Introduction to SharePoint Online Development - SharePoint Satur...
 
Extending SharePoint 2010 to your customers and partners
Extending SharePoint 2010 to your customers and partnersExtending SharePoint 2010 to your customers and partners
Extending SharePoint 2010 to your customers and partners
 
Advanced BCS - Business Data Connectivity Models and Custom Connectors - SPTe...
Advanced BCS - Business Data Connectivity Models and Custom Connectors - SPTe...Advanced BCS - Business Data Connectivity Models and Custom Connectors - SPTe...
Advanced BCS - Business Data Connectivity Models and Custom Connectors - SPTe...
 
Instant ECM with SharePoint 2010 - SPTechCon Boston 2011
Instant ECM with SharePoint 2010 - SPTechCon Boston 2011Instant ECM with SharePoint 2010 - SPTechCon Boston 2011
Instant ECM with SharePoint 2010 - SPTechCon Boston 2011
 
SharePoint 2010 Development for ASP.NET Developers - SharePoint Saturday Hous...
SharePoint 2010 Development for ASP.NET Developers - SharePoint Saturday Hous...SharePoint 2010 Development for ASP.NET Developers - SharePoint Saturday Hous...
SharePoint 2010 Development for ASP.NET Developers - SharePoint Saturday Hous...
 
Getting the most ouf of SharePoint Search - Tulsa SharePoint Interest Group
Getting the most ouf of SharePoint Search - Tulsa SharePoint Interest GroupGetting the most ouf of SharePoint Search - Tulsa SharePoint Interest Group
Getting the most ouf of SharePoint Search - Tulsa SharePoint Interest Group
 
Instant ECM with SharePoint 2010
Instant ECM with SharePoint 2010Instant ECM with SharePoint 2010
Instant ECM with SharePoint 2010
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Introduction to LINQ To XML

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

Notes de l'éditeur

  1. Talk about demo of purchase orders over $40.00
  2. Talk about the anonymous type we will be creating.
  3. Item Count
  4. Item Count