Response.Encoding = Encoding.GetEncoding(936) 設定 GB2312。Response.ContentType 決定瀏覽器解析模式(如 application/vnd.ms-excel 觸發 Excel 開啟)。深入 Web 開發需理解 Windows 登錄與 COM 關聯機制。
XmlTextWriter writer = new XmlTextWriter(Response.Output);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;
writer.IndentChar = ' ';
縮排僅為人類閱讀方便,程式解析無影響。
XmlTextWriter 內建語法檢查,確保輸出合格。所有 WriteStartXxx 必須與對應 WriteEndXxx 配對,且 WriteStartDocument 必為第一個呼叫。
string xsl = Request.QueryString["xsl"];
if (!string.IsNullOrEmpty(xsl))
writer.WriteProcessingInstruction("xml-stylesheet", $"type='text/xsl' href='{xsl}'");
瀏覽器讀取此指令自動下載 XSLT 轉換顯示。