|
|
Browse by Tags
All Tags » Useful Code Snippets (RSS)
-
[Blog Map] Excel has a very cool feature where you can declare that a range of cells is a table. It is a feature that allows you to use Excel very much like a database. You can add new rows as necessary, sort the table by columns, do some simple filtering, calculate the sum of columns, and more. Each table has a unique table name, and each column has a name unique to its table. Because these tables are stored in Open XML documents, we can implement some simple extension methods and some classes so Read More...
|
-
When writing queries, just as you sometimes want to skip the first n items in a collection, on occasion you want to skip the last n items. You could certain count the items remaining in the collection, and use the Take operator to take all but the last, but this would mean iterating the collection twice, and would result in uglier code. This post presents the SkipLast Extension method, which allows you to skip the last n items. In the next post, I’m going to present an enhancement to LtxOpenXml classes, Read More...
|
-
[Blog Map] A convenient way to explore Open XML markup is to create a small document, modify the document slightly in the Word user interface, save it, and then compare it with the Open XML Diff utility that comes with the Open XML SDK V2 . However, Word adds extraneous elements and attributes that enable merging of two documents that have forked. These elements and attributes show up as changed, and obscure the differences that we’re looking for. An easy way to deal with this is to remove these Read More...
|
-
Data-bound content controls are a powerful and convenient way to separate the semantic business data from the markup of an Open XML document. After binding content controls to custom XML, you can query the document for the business data by looking in the custom XML part rather than examining the markup. Querying custom XML is much simpler than querying the document body. However, it’s a little bit involved to create data-bound content controls (but only a little bit). But there is a trick we can Read More...
|
-
Every now and then, I need to have a source of data for an example or proof of concept that I’m building. I my focuses on LINQ to XML, Open XML, and now SharePoint, It’s pretty rare that I have to directly use a SQL database. I only do so maybe once or twice a year, and each time it comes up, I can’t remember where to get the database, and the easiest way to get started. This post is a cheat-sheet for the future - posting this just so that the next time it comes up, I can get going faster. Download Read More...
|
-
You may optionally be making a number of modifications to a very large XDocument object. Because of a complicated algorithm, you may not necessarily know ahead of time whether you will be making changes. If you don’t make any changes, then you don’t want to unnecessarily serialize the tree and save it to disk, or send it over the wire. This post presents a simple technique to determine if an XDocument has ever been modified since deserialization or creation. For example, you might have a very large Read More...
|
-
WSS normally provides error messages intended for end users. Modifying web.config enables debugging support and error messages that contain stack traces. No news here. I’m posting this snippet of web.config so that I can always find it when I need it. < configuration > < SharePoint > < SafeMode CallStack = " true " /> </ SharePoint > < system.web > < customErrors mode = " Off " /> < compilation debug = " true " /> </ system.web > </ configuration Read More...
|
-
LINQ is a great tool for writing ad-hoc queries and transforms, and occasionally I need to write queries or transforms on text files. And sometimes I receive CSV files, and need to do something with them. I wrote a blog post on LINQ to Text files over two years ago. My coding practices today differ from what I presented in that blog post. This post presents my current approach for dealing with text files using LINQ, and includes a function for splitting CSV lines. In that post, I detailed an approach Read More...
|
-
Sometimes you need to create a list of all parts in a package so that you can write some generalized code to deal with the parts. This post presents a bit of code that creates a list (List<OpenXmlPart>) of all parts in a package. Note that the parts in a package don’t form a tree – they form a graph. Any part can potentially have a relationship to any other part, which in turn can potentially have a relationship back to the first part. So the recursive code has to look and see if the part already Read More...
|
-
Content controls are an effective way to add structure to word processing documents. You can write a very small LINQ query to retrieve the contents of content controls. This topic in Office Online provides more information on content controls. In an upcoming post, I’m going to show how you can write a small test harness to test code that is embedded in documents. This is particularly useful, say, to a program manager who has written a specification that contains a lot of code that shows how to use Read More...
|
-
Raw data for a LINQ query doesn’t always come in the form you want. Recently, I had some data like this: string [] source = new [] { "EW" , "Eric" , "8:00" , "DW" , "Dave" , "9:00" , "JA" , "Jim" , "8:00" }; You want to transform the above collection of strings into a collection of anonymous objects that are easier to work on, so you need to write a query that operates on groups of three. To solve this problem, you can take advantage that the Select extension method has an overload that passes an Read More...
|
-
You can use lambda expressions to write an event handler, even for classes that predate C# 3.0 and the latest version of the framework. This can shorten your code, and make it easier to read. For example, the code to validate an XML tree using an XSD schema takes an event handler because it may return multiple errors or warnings for a single validation pass. An easy way to write this code is as follows: using System; using System.Linq; using System.Collections.Generic; using System.IO; using System.Xml; Read More...
|
-
Sometimes you need to find the duplicates in a list. I’m currently developing a little utility that tests code in a word processing document (to be blogged soon). Each code snippet in the document has an identifier, and one of the rules that I’m imposing on this code testing utility is that there should be no duplicate identifiers in the set of documents that contain code snippets to be tested. An easy way to find duplicates is to write a query that groups by the identifier, and then filter for groups Read More...
|
-
Sometimes in the middle of a .NET application (either C# or VB) you want to run an executable and collect the output. This post presents a simple function (RunExecutable) that makes it easy to do this. I write a lot of documents that contain code. Many of those documents also contain the expected output from the code. When you have a very large document with many code snippets, it is useful to automate the testing of those snippets. It gives me a warm and fuzzy feeling to verify that all of the code Read More...
|
|
|
|