posts - 72, comments - 66, trackbacks - 0, articles - 0
          About Exception:
          An invalid XML character (Unicode: 0x0) was found in the element content of the document.

          問題描述:
          當我們用
          byte[] info ;
          DocumentHelper.parseText(new String(info));
          將一個字節數組轉成字符串再轉成Document(XML格式)時,常常會遇到上述異常。
          特別是當字符串有加、解密,或編碼等情況時。

          原因:
          從異常來看,很明顯是因為字節數組中存在 Unicode: 0x0,而這個字節在Xml中被認為是非法字符。

          對于一些經過編碼或加、解密的字符串中,很容易會出現這個 0x0,
          特別是在加、解密中,經常會涉及到字符填充,而填充物通常是 0x0,
          需對于0x00-0x20 都會引起一定的問題,又因為這些字符不可見,因此用通常的編輯器進行編輯的時候找不到問題所在。
          而在轉成String后也覺察不到任何異常。
          所以在轉成XML格式時要對字符串進行檢測:
          *  Verify that no character has a hex value greater than 0xFFFD, or less than 0x20.
          * Check that the character is not equal to the tab ("t), the newline ("n), the carriage return ("r), or is an invalid XML character below the range of 0x20. If any of these characters occur, an exception is thrown.

          pubic void CheckUnicodeString(String value)
              {
              for (int i=0; i < value.Length; ++i) {
                  if (value[i] > 0xFFFD)
                  {
                      throw new Exception("Invalid Unicode");//或者直接替換掉0x0 value[i]='"n';
                  }
                  else if (value[i] < 0x20 && value[i] != '"t' & value[i] != '"n' & value[i] != '"r')
                  {
                      throw new Exception("Invalid Xml Characters");//或者直接替換掉0x0 value[i]='"n';
                  }
              }

          相關資源:
          http://msdn.microsoft.com/en-us/library/k1y7hyy9.aspx
          http://gceclub.sun.com.cn/developer/technicalArticles/Intl/Supplementary/index_zh_CN.html

          主站蜘蛛池模板: 石河子市| 上思县| 陆川县| 沁阳市| 乐业县| 禄劝| 徐州市| 隆德县| 阿图什市| 从化市| 海原县| 沙田区| 富裕县| 绥化市| 高淳县| 西丰县| 三河市| 广汉市| 通海县| 寿阳县| 阜平县| 镇安县| 大姚县| 儋州市| 巍山| 罗田县| 丹东市| 读书| 四子王旗| 巴青县| 濮阳市| 邮箱| 玉山县| 五台县| 荥阳市| 崇左市| 玛纳斯县| 富锦市| 宁化县| 孟州市| 长寿区|