In a string occurance of particular character can be found easily by using the function 'InStr()' which returns the location of the character in the string. But if we want to count the number of occurances of particular character in the string then I've found the following three ways to get the
Private Function countOccurance1(ByVal hayStack As String,ByVal needle As Char) As Integer
If String.IsNullOrEmpty(hayStack) Then Return 0 Dim chars() As Char = hayStack.ToCharArray Dim count As Integer = 0 For Each ch As Char In chars If ch = needle Then count += 1 Next Return count
End Function
Private Function countOccurance2(ByVal hayStack As String, ByVal needle As String) As Integer Return (Len(hayStack) - Len(Replace(hayStack, needle, ""))) / Len(needle) End Function
Private Function countOccurance3(ByVal hayStack As String, ByVal needle As String) As Integer
Dim i As Long, sHayStack As String : sHayStack = hayStack Do Until InStr(sHayStack, needle) = 0 sHayStack = Replace(sHayStack, needle, "", 1, 1) : i += 1 Loop Return i
End Function
I've not tested which is more effiecient, but all provides the similar results for many number tests.
Open XML documents provides strongly typed part and content classes for use
Open XML is an open ECMA 376 standard and is also approved as the ISO/IEC 29500 standard that defines a set of XML schemas for representing spreadsheets, charts, presentations, and word processing documents. Microsoft Office Word 2007, Excel 2007, and PowerPoint 2007 all use Open XML as the default file format.
The Open XML file formats are useful for developers because they use an open standard and are based on well-known technologies: ZIP and XML.
The Open XML Format SDK 2.0 is built on top of the System.IO.Packaging API and provides strongly typed part classes to manipulate Open XML documents. The SDK also uses the .NET Framework Language-Integrated Query (LINQ) technology to provide strongly typed object access to the XML content inside the parts of Open XML documents.
Requirements:
Supported Operating Systems: Windows Server 2003; Windows Vista; Windows XP Service Pack 2
This download requires the following:
The Microsoft .NET Framework version 3.5.
Up to 120 MB of available disk space.
“Be aware that it’s a 90MB download (about 120MB installed). About 100MB of it is documentation. The library itself (DocumentFormat.OpenXml.dll) is 3.5MB” as specified in windowsclientdevelopment.
The Open XML Format SDK 2.0 went live on April 7, 2009, and is now up for grabs via the Microsoft Download Center. The SDK, and the underlying System.IO.Packaging API enables developers to leverage a simplified manipulation process involving OpenXML packages. And when it comes down to manipulation of OpenXML packages, version 2.0 of the SDK brings to the table support for strongly typed part and content classes.
“In version 1 of the Open XML SDK we released the Open XML Packaging API, which allows you to create, open and manipulate Open XML files at the package and part level. In the first CTP of version 2 we released the Open XML Low Level DOM and Stream Reading/Writing components, which allow you to create and manipulate objects within xml parts contained in an Open XML package. In the second CTP of version 2 we are providing schema level validation functionality,” revealed Brian Jones, Office program manager.
In addition, the April 2009 CTP of Open XML Format SDK 2.0 permits OpenXML documents to be validated. The validation functionality added to the latest version of the SDK is designed to permit developers to perform validation of Open XML docs in relation to variations of the Open XML Format. Essentially what validation support is designed to do is to help reduce the possibility of invalid or corrupt Open XML files.
“Manipulating Open XML Formats by using the Open XML Base layer makes it much easier for you to work on the Open XML files, but doing so does not guarantee the production of valid Open XML files. The new Schema Level Validation component provides a mechanism to help you discover Open XML errors within files and in your code. This component assists you in debugging and validating Open XML files based on the schemas,” Jones added.
This release includes a high-level DOM (document object model) for Open XML development, as well as several tools to streamline Open XML development:
* The OpenXmlDiff utility identifies differences in the markup in two Open XML documents.
* The Open XML Class Explorer helps you determine which strongly typed class to use for a specific task, and includes the text of the relevant section of the ECMA-376 spec for each class.
* The Open XML Document Reflector takes a target document as input, and with a few clicks it shows you the C# code needed to generate that document (or a section of it) with the Open XML SDK.
Sample code that supports the scenarios described in the series of articles titled Creating Documents by Using the Open XML Format SDK 2.0 (Community Technology Preview)
In case we need to fill a DataGridView in our form with all the files in the specified directory, no matter how deep many files exists, all the files are retireved from the directory and the related information is displayed in the DataGridView.
Dim files = From file In My.Computer.FileSystem.GetFiles(rootDir, FileIO.SearchOption.SearchTopLevelOnly) _
Order By file _
Select My.Computer.FileSystem.GetFileInfo(file)
arrFileList.AddRange(files.ToList)
End Sub
This function return a collection of System.IO.FileInfo objects that can be used to retrieve all files and their corresponding information with same list that can be used depending on the requirement.
If only the specific attribute is desired for each file, then we can specify the needed attribute under the Select Key
For Each foundFile As String In My.Computer.FileSystem.GetFiles(rootDir,FileIO.SearchOption_ .SearchTopLevelOnly)
arrFileList.Add(foundFile)
Next
End Sub
rootDir can be root drive or some specific directory. arrFileList is the final list of files in rootDir' all subDir,Sub-SubDir,Sub-Sub-SubDir................ means all the existing files, no matter how much deeper, are added in th earrFileList that can be used for any needed purpose requiring all the list of files in some dir.
and Let bytes() be the required converted picture into byte. Dim bytes() As Byte
Then the code to convert the picture to bytes as below:
If picture IsNot Nothing Then Dim BitmapConverter As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(picture.[GetType]()) bytes= DirectCast(BitmapConverter.ConvertTo(picture, GetType(Byte())), Byte()) Else bytes= Nothing End If
Also to get picture back to bitmap format from the byte object the code will be like as below:
If bytes IsNot Nothing Then pic= New Bitmap(New MemoryStream(value)) Else pic = Nothing End If
Uses: The picture can't save directly in XML file and is required to be converted to like bytes only then can be saved to XML file. Similarly later it can be retrieved back to picute in bitmap format by using the above procedure.