|
|
Browse by Tags
All Tags » LINQ and LINQ to XML (RSS)
-
Some time ago, I was talking to some members of a team that used LINQ and LINQ to XML in one particular area of their code. They said that the code written using a conventional approach to XML was in the order of 6000 lines of code. When re-written using LINQ and LINQ to XML, it was around 800 lines of code. It was faster to code, and it was easier to debug. And there is some correlation between lines of code and bugs regardless of the type of code , so reducing lines of code means better software. Read More...
|
-
This is a bit of a geeky post for the LINQ and LINQ to XML folks. This post introduces a GroupAdjacent generic extension method that groups elements in a collection with adjacent elements based on a common key. For example, grouping the following array creates six groups (not 3, as with GroupBy): int[] ia = new int[] { 1, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 }; GroupAdjacent groups them like this: Group 1 1 Group 0 0 0 Group 2 2 Group 0 0 0 0 Group 2 2 Group 0 0 0 0 In contrast, the standard GroupBy extension Read More...
|
-
It can be problematic for us here at Microsoft to make specific performance claims of one technology to another, for example, comparing the performance of XmlDocument, say, to LINQ to XML. A good programmer can write code in any number of technologies...( read more ) Read More...
|
-
I've received some comments indicating that XSLT is better for these types of transformations. FWIW, I absolutely agree about the usage scenarios for XSLT. In the LINQ to XML documentation, I have at least 4 or 5 examples that show how to use XSLT to transform an XML tree. XSLT transforms create a new tree, so XSLT does not aleviate the problems of too many short-lived objects. With respect to processor cost, I haven't done any metrics, however, when doing XSLT transforms using LINQ to XML, the XML Read More...
|
-
Introduction Annotations can be used to facilitate transforms of an XML tree. Some XML documents are "document centric with mixed content." With such documents, you don't necessarily know the shape of child nodes of an element. For instance, a node that contains text may look like this: [xml] <text>A phrase with <b>bold</b> and <i>italic</i> text.</text> For any given text node, there may be any number of child <b> and <i> elements. This approach extends Read More...
|
-
Quite some time ago, I wrote a blog post on how you can stream text files as input into LINQ queries by writing an extension method that yields lines using the yield return statement. You then can write a LINQ query that processes the text file in a lazy deferred fashion. If you then use the T:System.Xml.Linq.XStreamingElement to stream output, you then can create a transform from the text file to XML that uses a minimal amount of memory, regardless of the size of the source text file. You can transform Read More...
|
-
We often use the Visitor pattern to separate the structure of an object tree or collection from the operations performed on that tree or collection. There are lots of ways in LINQ where you can visit some function on a collection, but when implementing this pattern as a couple of extension methods in LINQ to XML, we can pass the element depth as an argument to the delegate. This allows us to do interesting things because we know the element depth. For instance, we can use the Visit method to transform Read More...
|
-
You can, of course, use C# anonymous types to create types that are more than simple tuples. For example, you can nest anonymous types to create an object graph: var PurchaseOrder = new { PurchaseOrderNumber = "99503", OrderDate = DateTime.Parse("1999-10-20"), Addresses = new [] { new { AddressType = "Shipping", Name = "Alice Smith", Street = "123 Maple Street", City = "Mill Valley", State = "CA", Zip = "90952", Country = "USA" }, new { AddressType = "Billing", Name = "Robert Smith", Street = "8 Read More...
|
-
Following are a few additional notes regarding the Linq to Text Files example. Taking Advantage of Multiple CPUs If you have some type of computing where you need to process large text files, and the processing of the text files is processor intensive, the possibility exists in the future where you could factor your processing into separate LINQ queries and perhaps a future version of LINQ could parcel out each query to a separate CPU. You could take advantage of parallel processors without extensive Read More...
|
-
Lazy evaluation is an important technique in functional programming. There is a entertaining article on functional programming here . I was thinking about lazy evaluation the other day, and the issues of processing huge text files using streaming techniques, and I realized that LINQ could do really cool things if I implemented an extension method for StreamReader that iterated over the lines of a text file. Because of the lazy evaluation, provided you construct only certain queries, this technique Read More...
|
-
Recently, I had a problem where there wasn't a code testing harness that would do exactly what I wanted. I want to grab my code snippet directly from my word document, compile it, run it, and validate the output. In more technical terms, I want to parse some WordML to grab text formatted with a given style. Further, I want to put a comment on the first line of the formatted text, and be able to grab the comment. The comment will contain the metadata that tells how to compile and run the code. My Read More...
|
-
It is an interesting process to come up to speed on LINQ and XLinq. As the programming writer who works on XLinq, I fortunately have the capacity to affect how programmers learn about XLinq. One of my pet peeves is documentation that doesn't teach. Too often, documentation is a mishmash of stuff aranged in a hierarchy that is oriented more towards finding out how to do a particular task. No doubt, this is useful, but it is NOT the only usage scenario for documentation, to say the least. For the May Read More...
|
-
Introduction This post contains the nomenclature that I used in the XLinq documentation for the May 2006 CTP. Note that this is not the official list. It may change in the future. query expression This is the term for what has been called "query comprehension" in the past. An example of a query expression is: var z = from c in contacts.Elements() where (string)c.Element("address").Element("state") == "WA" select (string)c.Element("name"); expression When you use the "explicit dot notation", it is Read More...
|
-
It's my opinion that LINQ and XLinq will benefit from having patterns named. The coding techniques are involved enough that we should have names for them. This is my first take on what the patterns should be. I'll be adding more in the future. Some of these patterns may be too simple to be called patterns. I've had the suggestion of calling them idioms. Another idea is to call them "notations". In any case, it is good to have names for them. For example, instead of telling another programmer, oh, Read More...
|
|
|
|