首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 發現之旅第一講:XML 開發(五)

     編輯:關於C語言

HTTP 輸出設定

  • Response.Encoding = Encoding.GetEncoding(936) 設定 GB2312。
  • Response.ContentType 決定瀏覽器解析模式(如 application/vnd.ms-excel 觸發 Excel 開啟)。

深入 Web 開發需理解 Windows 登錄與 COM 關聯機制。

XmlTextWriter 初始化與縮排


XmlTextWriter writer = new XmlTextWriter(Response.Output);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;
writer.IndentChar = ' ';

縮排僅為人類閱讀方便,程式解析無影響。

為何不直接字串拼 XML?

  • XML 語法嚴格,單一錯誤導致整份文件解析失敗。
  • XmlTextWriter 內建語法檢查,確保輸出合格。
  • HTML 容錯率高,字串拼湊勉強可行;XML 強烈建議用工具類別。

WriteStartDocument 配對規則

所有 WriteStartXxx 必須與對應 WriteEndXxx 配對,且 WriteStartDocument 必為第一個呼叫。

XSLT 樣式表指令輸出


string xsl = Request.QueryString["xsl"];
if (!string.IsNullOrEmpty(xsl))
    writer.WriteProcessingInstruction("xml-stylesheet", $"type='text/xsl' href='{xsl}'");

瀏覽器讀取此指令自動下載 XSLT 轉換顯示。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved