本文以C#及VB.NET后端程序代码示例展示如何将HTML转为XML文件。转换时,调用Word API -Free Spire.Doc for .NET 提供的文档加载方法及文档保存的方法来实现。转换的代码步骤很简单,具体可参考以下内容。
引入dll
1.通过NuGet安装dll(2种方法)
1.1可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
1.2将以下内容复制到PM控制台安装:
Install-Package FreeSpire.Doc -Version 10.2
2.手动添加dll引用
可通过手动下载包到本地,然后解压,找到BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
将HTML转为XML
转换时,可参考如下代码来进行:
- 创建Document类的对象。
- 调用Document.LoadFromFile(string fileName, FileFormat fileFormat)方法加载HTML文档。
- 通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存为XML格式到指定路径。
C# - using Spire.Doc;
- namespace HTMLtoXML
- {
- class Program
- {
- static void Main(string[] args)
- {
- //创建Document类的对象
- Document doc = new Document();
- //加载html文件
- doc.LoadFromFile("sample.html",FileFormat.Html);
- //保存为XML格式
- doc.SaveToFile("HTMLtoXML.xml", FileFormat.Xml);
- System.Diagnostics.Process.Start("HTMLtoXML.xml");
- }
- }
- }
复制代码 vb.net- Imports Spire.Doc
- Namespace HTMLtoXML
- Class Program
- Private Shared Sub Main(args As String())
- '创建Document类的对象
- Dim doc As New Document()
- '加载html文件
- doc.LoadFromFile("sample.html", FileFormat.Html)
- '保存为XML格式
- doc.SaveToFile("HTMLtoXML.xml", FileFormat.Xml)
- System.Diagnostics.Process.Start("HTMLtoXML.xml")
- End Sub
- End Class
- End Namespace
复制代码 转换效果:

—End—
来源:https://www.cnblogs.com/Yesi/archive/2022/06/15/16378661.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |