来自参考来源 http://referencesource.microsoft.com/#System.Xml.Linq/System/Xml/Linq/XLinq.cs,3e5abc19cad1911d,references for XElement
,看起来好像名称空间/前缀属性对被推送到下推堆栈 http://referencesource.microsoft.com/#System.Xml.Linq/System/Xml/Linq/XLinq.cs,84aa37da63f3d367,references按照写入时的添加顺序,然后检查与元素名称空间的匹配从上到下 http://referencesource.microsoft.com/#System.Xml.Linq/System/Xml/Linq/XLinq.cs,151541270dad5982,references堆栈的——有效地以与添加属性相反的顺序进行匹配。
因此,如果您以相反的顺序添加命名空间,ss
被省略:
XDocument xmlssDoc2 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new
XProcessingInstruction("mso-application", "Excel.Sheet"));
XNamespace blank = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XElement root = new XElementTest(blank + "Workbook");
root.Add(new XAttribute(XNamespace.Xmlns + "ss", ss));
root.Add(new XAttribute("xmlns", blank));
Debug.WriteLine(root.ToString());
产生:
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="urn:schemas-microsoft-com:office:spreadsheet" />
当然,这意味着xmlns
属性规范已经改变,但是理想情况下这应该没关系 http://www.w3.org/TR/REC-xml/#sec-starttags根据 XML 规范。