How to Convert Word Document to XML
Basic Knowledge about XML
When talking about XML, we may think of HTML. Actually, XML is similar to HTML are tag-based languages. The difference between XML and HTML is that the tags which XML uses are not predefined. If we want to create own tags within XML, we need to follow a few rules.
Firstly, only one root element is contained in XML document. The root element is often taken as document element and appears after the prolog section. Besides, all the XML elements should contain end tags. Both start and end tag should be identical. Also, the elements can’t overlap. What’s more, all attribute values must use quotation marks and we can’t use some special characters within the text. After following the rules, the XML document will be well formatted.
How to Convert Doc to XML in MS Word
Sometimes, we need to convert Doc file to XML document. How should we do to meet the requirement? We can use Save As in Word for Word provides the feature with saving Doc file as XML document. Select Save as in File menu, and then we can find the dialog box. Choose the Save Type as *.XML, and the Word document which you are editing can be saved as XML document.
How to Use Spire.Doc to Convert Doc to XML
Spire.Doc presents you an easy way to convert Doc to XML. In this way, you should create/load a word document, after performing all operations on it, you can use SaveToFile("Sample.xml", FileFormat.xml) metho to save the document to an XML format file.
The following code displays the way to convert Doc to XML:
[C#]
using
Spire.Doc;
using Spire.Doc.Documents;
namespace ToXML
{
class Program
{
static void Main(string[] args)
{
//Create word document
Document document = new
Document();
//Create section
Section section =
document.AddSection();
//Add a paragraph
Paragraph paragraph =
section.AddParagraph();
//Append text
paragraph.AppendText("Doc
to XML demo");
//Save XML file.
document.SaveToFile("Sample.xml",
FileFormat.Xml);
//Launching the XML file.
System.Diagnostics.Process.Start("Sample.xml");
}
}
}
[Visual Basic]
Imports
Spire.Doc
Imports Spire.Doc.Documents
Module Module1
Sub Main()
'Create word document
Dim document As New Document()
'Create section
Dim section As Section = document.AddSection()
'Add a paragraph
Dim paragraph As Paragraph =
section.AddParagraph()
'Append text
paragraph.AppendText("Doc to
XML demo")
'Save XML file.
document.SaveToFile("Sample.xml",
FileFormat.Xml)
'Launching the XML file.
Process.Start("Sample.xml")
End Sub
End Module
In : Word