使用XDocument生成XML Excel文档

我正在试图生成这样的XML Excel文档

<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <Styles> <Style ss:ID="Default" /></Style> </Styles> <Worksheet ss:Name="Worksheet Name"> <Table> <Row ss:AutoFitHeight="0"> <Cell> <Data ss:Type="String">Test</Data> </Cell> </Row> </Table> </Worksheet> </Workbook> 

我目前的结果是这样的:

 <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <Styles xmlns=""> <Style ss:ID="Default" /> </Styles> <Worksheet ss:Name="Worksheet Name" xmlns=""> <Table> <Row ss:AutoFitHeight="0"> <Cell> <Data ss:Type="String">Test</Data> </Cell> </Row> </Table> </Worksheet> </ss:Workbook> 

我已经尝试了很多不同的解决scheme,没有任何运气。 我需要用Workbookreplacess:Workbook,并删除所有的xmlns =“”。

我现在的代码看起来像这样(LinqPad):

 void Main() { XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet"; XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet"; var workbook = new XElement(ns + "Workbook"); workbook.Add(new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet")); workbook.Add(new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office")); workbook.Add(new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel")); workbook.Add(new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet")); workbook.Add(new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40")); var styles = new XElement("Styles"); workbook.Add(styles); var style = new XElement("Style"); style.Add(new XAttribute(ss + "ID", "Default")); styles.Add(style); var worksheet = new XElement("Worksheet"); worksheet.Add(new XAttribute(ss + "Name", "Worksheet Name")); workbook.Add(worksheet); var table = new XElement("Table"); worksheet.Add(table); var row = new XElement("Row"); row.Add(new XAttribute(ss + "AutoFitHeight", 0)); table.Add(row); var cell = new XElement("Cell"); var data = new XElement("Data"); data.Add(new XAttribute(ss + "Type", "String")); data.Value = "Test"; cell.Add(data); row.Add(cell); var document = new XDocument(new XDeclaration("1.0", "", null)); document.Add(new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"")); document.Add(workbook); var sw = new CustomStringWriter(); document.Save(sw); sw.ToString().Dump(); //sw.ToString().Replace("ss:Workbook", "Workbook").Replace("xmlns=\"\"", "").Dump(); } // Define other methods and classes here class CustomStringWriter : StringWriter { public override Encoding Encoding { get { return null; } } } 

有什么build议么?

你的问题很可能是你没有明白,默认的命名空间是从父元素inheritance。 拿这个XML来说吧,例如:

 <parent xmlns="http://namespace.com/uri"> <child /> </parent> 

在这里, child也有名称空间http://namespace.com/uri

所以你的StyleWorksheet元素等等都有xmlns=""的原因是你已经明确地说过这些没有命名空间。

您应该修改所有这些元素的创build,以包含正确的名称空间,例如:

 var styles = new XElement(ss + "Styles"); 

另外,我还会注意到,LINQ to XML支持比现在更多的声明式方法。 你可以通过构造函数传递所有元素的内容。 所以你可以重写你的风格位:

 var styles = new XElement(ss + "Styles", new XElement(ss + "Style", new XAttribute("ID", "Default") ) ); 

看到这个小提琴你的样本固定和重写在这种风格。