Boost Fertility New Invention Ups Success Rates.pdf
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
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
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 }