很妙的是,上面那個單引號所夾的,就是一個0x08 ASCII字元,而這個錯誤訊息被原原本本放到LogParser所產生的XML文件<Message>...</Message>Node中。導致的分析程式用XmlDocument.Load要載入LogParser產出的XML時,讀到ASCII 08,發生一模一樣的錯誤。
//分析結果
XmlDocument xd = new XmlDocument();
try
{ xd.Load(file);
}
catch (System.Xml.XmlException xe)
{ //將整個檔案讀成字串
string rawXml =
System.IO.File.ReadAllText(file);
string invalidCharsMatch =
"(?ims)[\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xb\xc\xe\xf" +
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19]";
//取代其中無效字元, 通通換成空字串
rawXml = System.Text.RegularExpressions.Regex.Replace(
rawXml,
invalidCharsMatch, "");
xd.LoadXml(rawXml);
}