XElement day = new XElement("2015-8-20"); // 非法名稱
root.Add(day); // 拋出 XmlException,直接跳出函數像 return
無 try-catch 時程序崩潰;有 try-catch 但未捕獲具體異常類型,表現為「語句未執行」。
XML 規範:元素名稱必須以字母或下劃線開頭,不能以數字、標點開始。`2015-8-20` 以 `2` 開頭,構造 `XElement` 即拋出 `System.Xml.XmlException`。
// 方案 1:屬性存日期
XElement day = new XElement("Day", new XAttribute("Date", "2015-8-20"));
// 方案 2:合法元素名 + 值
XElement day = new XElement("Date", "2015-8-20"); // 名稱 "Date" 合法
// 方案 3:編碼前綴(不推薦)
XElement day = new XElement("_2015-8-20");