DotNetNuke作為開源項目
我們經常需要把一個Text文件(如XML
什麼是文件編碼?
首先我們來復習一下編碼的基本概念
我可以不用關心這個麻煩的文件編碼嗎?
大致了解了什麼是文件編碼
在DNN裡
這裡要強調的一點是
如何解決文件編碼轉換的問題?
回到我們的問題
如何得知這個文件的編碼方式?
首先我們來看看如何得知文本文件的編碼方式
BOM
這裡涉及到一個BOM(Byte Order Mark) 的概念
對於Unicode
UTF
UTF
UTF
UTF
UTF
UTF
判定文件編碼方式
知道了這一點
看看DNN的代碼
GetTextEncodingType
Private Function GetTextEncodingType()Function GetTextEncodingType(ByVal Buffer As Byte()) As PaTextEncoding
UTF
UTF
UTF
UTF
Lets do the easy ones first
If Buffer(
Return PaTextEncoding
End If
If Buffer(
Return PaTextEncoding
End If
If Buffer(
Return PaTextEncoding
End If
This does a simple test to verify that there are no bytes with a value larger than
which would be invalid in UTF
Dim i As Integer
For i =
If Buffer(i) >
Return PaTextEncoding
End If
Next
Return PaTextEncoding
End Function
代碼很好懂
System
知道了編碼方式
UnicodeEncoding
ASCIIEncoding
UTF
UTF
UTF
等等
Encoding是基類
使用Encoding
使用GetString(Byte[])方法
復習了Sytem
PaTextEncoding EncodingType = GetTextEncodingType(Buffer);
string DecodedString =
switch (EncodingType)
{
case PaTextEncoding
DecodedString = System
break;
case PaTextEncoding
DecodedString = System
break;
case PaTextEncoding
DecodedString = System
break;
case PaTextEncoding
DecodedString = System
break;
case PaTextEncoding
throw new Exception(
break;
}
想法是沒錯的
DNN裡用了一個比較巧妙的辦法
這一部分代碼在DNN的DotNetNuke
GetAsciiString()函數實現轉換為ASCII編碼
Create two different encodings
Dim TargetEncoding As Encoding = Encoding
Perform the conversion from one encoding to the other
Dim asciiBytes As Byte() = Encoding
Convert the new byte[] into an ascii string
Dim asciiString As String = System
Return asciiString
End Function
根據不同的編碼方式
Select Case sqlFile
Case PaTextEncoding
strScript = GetAsciiString(sqlFile
Case PaTextEncoding
strScript = GetAsciiString(sqlFile
Case PaTextEncoding
strScript = GetAsciiString(sqlFile
Case PaTextEncoding
strScript = GetAsciiString(sqlFile
Case PaTextEncoding
Throw New Exception(String
End Select
This check needs to be included because the unicode Byte Order mark results in an extra character at the start of the file
The extra character
If strScript
strScript = strScript
End If
最後的一點問題
DNN裡這種避免BOM影響解碼的方法有一個問題
我想解決方案是
From:http://tw.wingwit.com/Article/program/ASP/201311/21682.html