我想要做的就是将我的数据表导出到Excel的XML文档。
所以我需要的是这样的东西:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />
我正在使用System.Xml.Linq
,我几乎拥有它,但我的代码不断在工作簿的前面添加“ss”。这是我的代码:
XDocument xmlssDoc2 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new
XProcessingInstruction("mso-application", "Excel.Sheet"));
XNamespace aw = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace fc = "urn:schemas-microsoft-com:office:spreadsheet";
XElement root = new XElement(aw + "Workbook",
new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"),
new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet")
);
我得到的结果是:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />
请帮助!
答案 0 :(得分:0)
从XElement
ss
开始,看起来命名空间/前缀属性对在写入时按照添加顺序被推送到reference source,然后检查是否与元素匹配来自堆栈push-down stack的命名空间 - 有效地按照添加属性的相反顺序进行匹配。
因此,如果以相反的顺序添加名称空间,则省略 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
当然,这意味着{{1}}属性规范的顺序已经改变,但理想情况下这是top to bottom根据XML规范。