MSDN Magazine - March 2008 - (Page 27) Office 2007 Files and LINQ KEN GETZ n the June 2006 installment of this column, I described how you can programmatically interact with the Microsoft® 2007 Office system Open XML File Formats using classes in the System.IO.Packaging and System.Xml namespaces (see msdn.microsoft.com/msdnmag/issues/06/06/AdvancedBasics). As I was writing that column, I was aware of some new technologies that would make my code simpler, but they weren’t quite ready yet. Now that Visual Studio® 2008 has been released, I’ll revisit the code using LINQ to XML and the Community Technology Preview (CTP) edition of the Microsoft SDK for Open XML Formats, which you’ll need to download from go.microsoft.com/fwlink/?LinkId=106944 in order to compile and test the code presented here. These two technologies can change the way in which you access the parts of the 2007 Office system Open XML document (created by Microsoft Word, Excel®, and PowerPoint®) when retrieving or modifying data. As I described in the June 2006 column, in order to programmatically interact with the Office Open XML File Formats you must be able to navigate to the exact document part you require and then interact with the XML in that part once you retrieve a reference to it. Usually you use the System.IO.Packaging namespace to navigate through the file format’s hierarchy of parts. Once you get to the part that you want to program against, you can use any existing XML technology to interact with it; generally, you’ll either use the XmlDocument class or the XmlReader and XmlWriter classes. The classes in the System.IO.Packaging namespace certainly make it possible to navigate the file format’s hierarchy of parts, but because it’s so general (it works with any Open XML File Format, not just 2007 Office system documents), the code you write using this API is generally more repetitive than you might like. To make this navigation easier, members of the Office team at Microsoft have been working on an SDK that encapsulates the generalized System.IO.Packaging namespace, providing an API that’s focused directly on the 2007 Office system file formats. I decided to use this SDK, even though it’s currently only available as a CTP, because it so greatly simplifies code that works with Office documents. The 2007 Office system Open XML SDK only handles the navigation to the individual part of interest; it is not used for manipulating the part’s XML content to retrieve or modify the document information. In the earlier column, I used the XmlDocument class to work with document properties. Now, you can use LINQ to XML, a new feature in the .NET Framework 3.5 (exposed in Visual Studio 2008) to make working with the XML content far easier. I For this column, I’ll modify the WDSetCustomProperty code, which was one of the forty snippets I wrote when the 2007 Office system first launched (go.microsoft.com/fwlink/?LinkId=106943). Before investigating the code in this article, you might want to examine the original code snippet to see how it works. (You’ll find a walkthrough of the code, plus a video demonstration, at msdn2.microsoft.com/bb308936.) You should also be relaNow that Visual Studio tively comfortable with the 2008 has been released, 2007 Office system Open let’s code using LINQ to XML File Formats, so begin by reviewing my earlier XML and the CTP edition column. The code related of the Microsoft SDK for to the 2007 Office system Open XML Formats. Open XML SDK is fairly self-evident, as you’ll see when you review the sample code for this article. (In order to use the SDK, you’ll need to download and install it, and within your project, set a reference to the Microsoft.Office.DocumentFormat.OpenXml.dll assembly included in the SDK.) The final technology I’ll cover here, LINQ to XML, is a broad topic on its own, so all I’ll do is discuss how I’ve used it in the context of this column. Although much has already been written on this important subject, you may want to start with the video presentation “How Do I Get Started with LINQ to XML?” at msdn2.microsoft.com/bb466226. In creating the code sample for this column, 90 percent of my time was devoted to XML namespace manipulation. Visual Basic® provides some amazing support for LINQ to XML in its latest version, but it’s easy to get tangled up in namespace manipulation. The description of the feature at msdn2.microsoft.com/bb687701 will be useful to you—it certainly helped me work things out! Getting Started In order to set or retrieve Word 2007 custom document properties, you’ll need to work with the docProps/custom.xml part. As you attempt to set a property, one of four situations can occur: • The custom property part may not yet exist, in which case, you must create it. • The custom property may not exist, in which case, you must create it. This column is based in part on a prerelease version of the 2007 Office system Open XML SDK. Details herein are subject to change. march2008 27 http://go.microsoft.com/fwlink/?LinkId=106943 http://msdn.microsoft.com/msdnmag/issues/06/06/AdvancedBasics http://msdn.microsoft.com/msdnmag/issues/06/06/AdvancedBasics http://msdn2.microsoft.com/bb308936 http://msdn2.microsoft.com/bb308936 http://go.microsoft.com/fwlink/?LinkId=106944 http://msdn2.microsoft.com/bb466226 http://msdn2.microsoft.com/bb687701
Table of Contents Feed for the Digital Edition of MSDN Magazine - March 2008 MSDN Magazine - March 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Office Space Introducing ASP.NET MVC Loosen Up CI Server Performance Office Development Test Run Security Briefs Extreme ASP.NET Foundations .NET Matters {End Bracket} MSDN Magazine - March 2008 MSDN Magazine - March 2008 - (Page Intro) MSDN Magazine - March 2008 - Contents (Page Cover1) MSDN Magazine - March 2008 - Contents (Page Cover2) MSDN Magazine - March 2008 - Contents (Page 1) MSDN Magazine - March 2008 - Contents (Page 2) MSDN Magazine - March 2008 - Contents (Page 3) MSDN Magazine - March 2008 - Contents (Page 4) MSDN Magazine - March 2008 - Contents (Page 5) MSDN Magazine - March 2008 - Contents (Page 6) MSDN Magazine - March 2008 - Contents (Page 7) MSDN Magazine - March 2008 - Contents (Page 8) MSDN Magazine - March 2008 - Contents (Page 9) MSDN Magazine - March 2008 - Contents (Page 10) MSDN Magazine - March 2008 - Toolbox (Page 11) MSDN Magazine - March 2008 - Toolbox (Page 12) MSDN Magazine - March 2008 - Toolbox (Page 13) MSDN Magazine - March 2008 - Toolbox (Page 14) MSDN Magazine - March 2008 - CLR Inside Out (Page 15) MSDN Magazine - March 2008 - CLR Inside Out (Page 16) MSDN Magazine - March 2008 - CLR Inside Out (Page 17) MSDN Magazine - March 2008 - CLR Inside Out (Page 18) MSDN Magazine - March 2008 - CLR Inside Out (Page 19) MSDN Magazine - March 2008 - CLR Inside Out (Page 20) MSDN Magazine - March 2008 - Data Points (Page 21) MSDN Magazine - March 2008 - Data Points (Page 22) MSDN Magazine - March 2008 - Data Points (Page 23) MSDN Magazine - March 2008 - Data Points (Page 24) MSDN Magazine - March 2008 - Data Points (Page 25) MSDN Magazine - March 2008 - Data Points (Page 26) MSDN Magazine - March 2008 - Advanced Basics (Page 27) MSDN Magazine - March 2008 - Advanced Basics (Page 28) MSDN Magazine - March 2008 - Advanced Basics (Page 29) MSDN Magazine - March 2008 - Advanced Basics (Page 30) MSDN Magazine - March 2008 - Advanced Basics (Page 31) MSDN Magazine - March 2008 - Advanced Basics (Page 32) MSDN Magazine - March 2008 - Office Space (Page 33) MSDN Magazine - March 2008 - Office Space (Page 34) MSDN Magazine - March 2008 - Office Space (Page 35) MSDN Magazine - March 2008 - Office Space (Page 36) MSDN Magazine - March 2008 - Office Space (Page 37) MSDN Magazine - March 2008 - Office Space (Page 38) MSDN Magazine - March 2008 - Office Space (Page 39) MSDN Magazine - March 2008 - Office Space (Page 40) MSDN Magazine - March 2008 - Office Space (Page 41) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 42) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 43) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 44) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 45) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 46) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 47) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 48) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 49) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 50) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 51) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 52) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 53) MSDN Magazine - March 2008 - Loosen Up (Page 54) MSDN Magazine - March 2008 - Loosen Up (Page 55) MSDN Magazine - March 2008 - Loosen Up (Page 56) MSDN Magazine - March 2008 - Loosen Up (Page 57) MSDN Magazine - March 2008 - Loosen Up (Page 58) MSDN Magazine - March 2008 - Loosen Up (Page 59) MSDN Magazine - March 2008 - Loosen Up (Page 60) MSDN Magazine - March 2008 - Loosen Up (Page 61) MSDN Magazine - March 2008 - Loosen Up (Page 62) MSDN Magazine - March 2008 - Loosen Up (Page 63) MSDN Magazine - March 2008 - Loosen Up (Page 64) MSDN Magazine - March 2008 - Loosen Up (Page 65) MSDN Magazine - March 2008 - Loosen Up (Page 66) MSDN Magazine - March 2008 - Loosen Up (Page 67) MSDN Magazine - March 2008 - Loosen Up (Page 68) MSDN Magazine - March 2008 - Loosen Up (Page 69) MSDN Magazine - March 2008 - CI Server (Page 70) MSDN Magazine - March 2008 - CI Server (Page 71) MSDN Magazine - March 2008 - CI Server (Page 72) MSDN Magazine - March 2008 - CI Server (Page 73) MSDN Magazine - March 2008 - CI Server (Page 74) MSDN Magazine - March 2008 - CI Server (Page 75) MSDN Magazine - March 2008 - CI Server (Page 76) MSDN Magazine - March 2008 - CI Server (Page 77) MSDN Magazine - March 2008 - CI Server (Page 78) MSDN Magazine - March 2008 - CI Server (Page 79) MSDN Magazine - March 2008 - CI Server (Page 80) MSDN Magazine - March 2008 - Performance (Page 81) MSDN Magazine - March 2008 - Performance (Page 82) MSDN Magazine - March 2008 - Performance (Page 83) MSDN Magazine - March 2008 - Performance (Page 84) MSDN Magazine - March 2008 - Performance (Page 85) MSDN Magazine - March 2008 - Performance (Page 86) MSDN Magazine - March 2008 - Performance (Page 87) MSDN Magazine - March 2008 - Performance (Page 88) MSDN Magazine - March 2008 - Office Development (Page 89) MSDN Magazine - March 2008 - Office Development (Page 90) MSDN Magazine - March 2008 - Office Development (Page 91) MSDN Magazine - March 2008 - Office Development (Page 92) MSDN Magazine - March 2008 - Office Development (Page 93) MSDN Magazine - March 2008 - Office Development (Page 94) MSDN Magazine - March 2008 - Office Development (Page 95) MSDN Magazine - March 2008 - Office Development (Page 96) MSDN Magazine - March 2008 - Test Run (Page 97) MSDN Magazine - March 2008 - Test Run (Page 98) MSDN Magazine - March 2008 - Test Run (Page 99) MSDN Magazine - March 2008 - Test Run (Page 100) MSDN Magazine - March 2008 - Test Run (Page 101) MSDN Magazine - March 2008 - Test Run (Page 102) MSDN Magazine - March 2008 - Test Run (Page 103) MSDN Magazine - March 2008 - Test Run (Page 104) MSDN Magazine - March 2008 - Test Run (Page 105) MSDN Magazine - March 2008 - Test Run (Page 106) MSDN Magazine - March 2008 - Security Briefs (Page 107) MSDN Magazine - March 2008 - Security Briefs (Page 108) MSDN Magazine - March 2008 - Security Briefs (Page 109) MSDN Magazine - March 2008 - Security Briefs (Page 110) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 111) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 112) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 113) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 114) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 115) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 116) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 117) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 118) MSDN Magazine - March 2008 - Foundations (Page 119) MSDN Magazine - March 2008 - Foundations (Page 120) MSDN Magazine - March 2008 - Foundations (Page 121) MSDN Magazine - March 2008 - Foundations (Page 122) MSDN Magazine - March 2008 - Foundations (Page 123) MSDN Magazine - March 2008 - Foundations (Page 124) MSDN Magazine - March 2008 - Foundations (Page 125) MSDN Magazine - March 2008 - Foundations (Page 126) MSDN Magazine - March 2008 - Foundations (Page 127) MSDN Magazine - March 2008 - Foundations (Page 128) MSDN Magazine - March 2008 - .NET Matters (Page 129) MSDN Magazine - March 2008 - .NET Matters (Page 130) MSDN Magazine - March 2008 - .NET Matters (Page 131) MSDN Magazine - March 2008 - {End Bracket} (Page 132) MSDN Magazine - March 2008 - {End Bracket} (Page Cover3) MSDN Magazine - March 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.