我正在使用以下简介使用 XElement 加载和保存 Excel XML 文件:
Dim root As XElement = XElement.Load(inFile)
'code to change elements goes here
root.Save(outFile)
问题是保存例程正在添加名称空间标记以及谁知道什么,因此 Excel 和 Windows 不再将其识别为 Excel XML 文件。在我的示例中,我什至没有操作元素。我只是加载和保存文件。我基本上想使用 linq 在 XML 中查找某些元素,更改它们然后保存整个文件。我做这太难了吗?
文件 XML
<?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">
<Worksheet ss:Name="Datagrid">
<Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row ss:Index="3" ss:AutoFitHeight="0">
<Cell ecProperty="email_address">
<Data ss:Type="String">email address</Data>
</Cell>
</Row>
<Row ss:Index="4" ss:AutoFitHeight="0">
<Cell ecProperty="synthesis_mode">
<Data ss:Type="String">Ideal Mode</Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
outFile 结果
<?xml version="1.0" encoding="utf-8"?>
<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">
<ss:Worksheet ss:Name="Datagrid">
<ss:Table ss:ExpandedColumnCount="13" ss:ExpandedRowCount="11" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
<ss:Row ss:Index="3" ss:AutoFitHeight="0">
<ss:Cell ecProperty="email_address">
<ss:Data ss:Type="String">email address</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row ss:Index="4" ss:AutoFitHeight="0">
<ss:Cell ecProperty="synthesis_mode">
<ss:Data ss:Type="String">Ideal Mode</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</ss:Worksheet>
</ss:Workbook>