Nii*_*ke2 2 .net c# excel linq-to-xml ms-office
我需要在C#和Linq中将这个XML复制到XML.除了正常的.NET之外,我不希望任何依赖于其他库.XML如下所示.
问题:我无法弄清楚如何打印这两行:
<?mso-application progid="Excel.Sheet"?>
<Data ss:Type="String">name</Data>
Run Code Online (Sandbox Code Playgroud)
完整的XML文档:
<?xml version="1.0" encoding="utf-8" ?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"></OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"></ExcelWorkbook>
<Worksheet ss:Name="Sheet 1">
<Table>
<Row>
<Cell>
<Data ss:Type="String">name</Data>
</Cell>
<Cell>
<Data ss:Type="String">status</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Niike2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Enabled</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Run Code Online (Sandbox Code Playgroud)
码:
XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", string.Empty),
new XComment(String.Format("Exported: {0}", DateTime.Now)),
new XElement(ns + "Workbook",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
new XElement(ns + "Worksheet",
new XElement(ns + "Table",
new XElement(ns + "Row",
new XElement(ns + "Cell", "name")
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
第二行用<?....?>被称为处理指令.其余的只是操纵命名空间.
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace x = "urn:schemas-microsoft-com:office:excel";
XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
XNamespace o = "urn:schemas-microsoft-com:office:office";
XNamespace html = "http://www.w3.org/TR/REC-html40";
XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", string.Empty),
new XComment(String.Format("Exported: {0}", DateTime.Now)),
new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),
new XElement("Workbook",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"),
new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
new XElement("Worksheet", new XAttribute(ss + "Name", "Sheet 1"),
new XElement("Table",
new XElement("Row",
new XElement("Cell",
new XElement("Data", new XAttribute(ss + "Type", "String"),"status"))
)
)
)
)
);
Run Code Online (Sandbox Code Playgroud)