在评论辩论Office OpenXML时,我们可能会想到HTML。实际上,Office OpenXML与HTML类似,都是基于标签的措辞。 Office OpenXML和HTML之间的差异是Office OpenXML利用的标签不是预定义的。 如果我们要在Office OpenXML中创建自己的标签,我们须要遵照一些规则。
首先,Office OpenXML文档中只包含一个根元素。 根元素常日被视为文档元素并涌如今媒介部分之后。 此外,所有的Office OpenXML元素都该当包含结束标签。 起始和结束标签该当是相同的。 而且,元素不能重叠。 更主要的是,所有属性值都必须利用引号,我们不能在文本中利用一些分外字符。 遵照规则后,Office OpenXML文档格式良好。
利用C#和VB.NET通过Spire.Doc将Doc转换为Office OpenXML

Spire.Doc(Spire.Office)为您供应了一种将Doc转换为Office OpenXML的大略方法,我们可以通过几次点击将现有Word文档转换为Office OpenXML格式,操作如下。
Step 1:创建项目
下载Spire.Doc并安装在系统上。 通过Visual Studio创建一个项目,并添加Spire.Doc DLL作为参考。
Note:请确保Spire.Doc和Visual Studio在系统上精确安装
Step 2:加载Word文件文件
加载我们须要转换为Office OpenXML格式确当地Word文档。
Document document = new Document;document.LoadFromFile(@\"大众D:\Sample.doc\公众);
Step 3:将Doc转换为Office OpenXML
Spire.Doc支持将Word Doc文件转换为大多数盛行的文件格式,如PDF,HTML,Office OpenXML,EPub,RTF,Dot,Text等。现在,利用以下代码将Word转换为Office OpenXML。
document.SaveToFile(\"大众Sample.xml\"大众, FileFormat.Xml);
Step 4:完全代码
现在,将完全的代码写入项目,然后按F5启动程序。
[C#]
using System;using System.Windows.Forms;using Spire.Doc;using Spire.Doc.Documents;namespace to XML{ public partial class Form1 : Form { public Form1 { InitializeComponent; } private void button1_Click(object sender, EventArgs e) { //Create word document Document document = new Document; document.LoadFromFile(@\公众D:\Sample.doc\"大众); //Save doc file. document.SaveToFile(\"大众Sample.xml\"大众, FileFormat.Xml); //Launching the MS Word file. WordDocViewer(\"大众Sample.xml\"大众); } private void WordDocViewer(string fileName) { try { System.Diagnostics.Process.Start(fileName); } catch { } } }
[VB.NET]
Imports SystemImports System.Windows.FormsImports Spire.DocImports Spire.Doc.DocumentsNamespace to XML Partial Public Class Form1 Inherits Form Public Sub New InitializeComponent End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) 'Create word document Dim document As New Document document.LoadFromFile(\"大众D:\Sample.doc\"大众) 'Save doc file. document.SaveToFile(\公众Sample.xml\公众, FileFormat.Xml); 'Launching the MS Word file. WordDocViewer(\"大众Sample.xml\"大众) End Sub Private Sub WordDocViewer(ByVal fileName As String) Try System.Diagnostics.Process.Start(fileName) Catch End Try End Sub End Class
运行演示后,您可能会在浏览器中创造Office OpenXML文档: