MSDN Magazine - March 2008 - (Page 29) download on the MSDN® Magazine Web site. You might want to refer to it as the text here walks through the fragments of the procedure. It begins by investigating the parameters, converting the input value as necessary, as shown in Figure 2. Based on the enumerated value you supply, indicating the type of the value, the code attempts to convert the Object value you’ve provided to the correct type. Note that in order for Word 2007 to accept the values, date/time values must be in Universal Time Coordinate (UTC) time (that is, based on Greenwich mean time) and Boolean values must be in lower case. If, after investigating the values, the propertyValueString variable is still Nothing, the code raises an exception back to the caller, ending the procedure. Next, the code uses the Microsoft SDK for Open XML Formats to find the document part and the custom properties part within the package. Compare this code to the code from the original snippet, and you’ll have to agree that the SDK makes it far easier to navigate within the document package: Using wdPackage = _ Packaging.WordprocessingDocument.Open(docName, True) Dim documentPart = wdPackage.MainDocumentPart Dim customPropsPart = wdPackage.CustomFilePropertiesPart End Using trieves the node’s first child element, using the First method (in this case, if the code can’t find the first child element, it raises an exception—the property node must contain an element, and if it doesn’t, the code can’t continue because the document is damaged). If the name of the child element matches the type of the new property value, the code changes the value of the element. If not, the code removes the node and sets the propertyNode variable to Nothing. After this code runs, the propertyNode variable either contains the new value or is set to Nothing, as you see in Figure 4. Figure 2 Checking the Types and Performing Conversions Dim propertyTypeName As String = Nothing Dim propertyValueString As String = Nothing ‘ Calculate the correct type: Select Case propertyType Case PropertyTypes.DateTime propertyTypeName = “filetime” ‘ Make sure you were passed a real date, ‘ and if so, format in the correct way. ‘ The date/time value passed in should ‘ represent a UTC date/time. If TypeOf (propertyValue) Is DateTime Then propertyValueString = String.Format(“{0:s}Z”, _ Convert.ToDateTime(propertyValue)) End If Case PropertyTypes.NumberInteger propertyTypeName = “i4” If TypeOf (propertyValue) Is Integer Then propertyValueString = propertyValue.ToString() End If Case PropertyTypes.NumberDouble propertyTypeName = “r8” If TypeOf propertyValue Is Double Then propertyValueString = propertyValue.ToString() End If Case PropertyTypes.Text propertyTypeName = “lpwstr” propertyValueString = propertyValue.ToString() Case PropertyTypes.YesNo propertyTypeName = “bool” If TypeOf propertyValue Is Boolean Then ‘ Must be lower case! propertyValueString = _ Convert.ToBoolean(propertyValue).ToString().ToLower() End If End Select If propertyValueString Is Nothing Then ‘ If the code wasn’t able to convert the ‘ property to a valid value, throw an exception: Throw New InvalidDataException(“Invalid parameter value.”) End If If the code is able to retrieve a reference to the custom properties part (that is, if the customPropsPart variable isn’t Nothing), the code loads the contents of the part using the XDocument.Load method (see Figure 3), allowing the GetStream method of the part to retrieve the XML contents. If the part doesn’t exist, the code uses the SDK to create the custom properties part. If the part does exist, the code also attempts to retrieve the first property node for which the name attribute matches the property name you’ve specified. Again, compare this to the original code snippet—you’ll be amazed how much easier this technique is. This code statement makes use of LINQ to XML and the Visual Basic axis properties to simplify the XML manipulation: propertyNode = _ (From node In customDoc. . _ Where node.@name = propertyName).FirstOrDefault() The “.” (period) separator indicates a node one level beneath the parent (customDoc). The syntax indicates an element name. The @ syntax indicates an attribute name. These are specific Visual Basic features (in C#, you would need to use the Element or Attribute property of the XDocument object to get the same information). The code also uses the FirstOrDefault LINQ extension method. This method returns the first matching node, or the default value (Nothing) if it finds no match. You could also use the First or Single extension methods, but each of those raises an exception if no match is found. Here, the code handles the case in which no match was found without raising an exception. After this code executes, customPropsPart contains either a reference to the custom properties part, or Nothing. Likewise, propertyNode contains either a reference to the XElement object corresponding to the property you want to modify, or Nothing if the property doesn’t yet exist. If the propertyNode variable isn’t equal to Nothing, the code re- Figure 3 Find the Custom Properties Part and Set Properties Dim customDoc As XDocument = Nothing Dim propertyNode As XElement = Nothing If customPropsPart IsNot Nothing Then ‘ Load the existing custom properties part: customDoc = XDocument.Load( _ New StreamReader(customPropsPart.GetStream())) propertyNode = _ (From node In customDoc. . _ Where node.@name = propertyName).FirstOrDefault() Else customPropsPart = wdPackage.AddCustomFilePropertiesPart End If Advanced Basics march2008 29
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.