Convert html String to docx xml string

Hi,

We are currently using:

Is there a way to convert html string to docx xml string?

Best,

Jeff

Hi Jeff,

Thanks for your inquiry. Please use following code example to convert Html string to Docx and WordML. Hope this helps you. Please let us know if you have any more queries.

string html = 
    "<html>
    <head>
        <title>
            Test
            html
        </title>
    </head>
    <body>
        <div>Test</div>
    </body>";
    
// Get bytes from string
byte[] htmlBytes = Encoding.UTF8.GetBytes(html);
    
// Create memory stream
MemoryStream htmlStream = new MemoryStream(htmlBytes);
    
// Create document
Document doc = new Document(htmlStream, new LoadOptions(LoadFormat.Html, "", ""));
    
// Create empty stream
MemoryStream xmlStream = new MemoryStream();
    
// Save document as WordML to stream
doc.Save(xmlStream, SaveFormat.WordML);
    
// Save document as Docx
doc.Save(MyDir + "Out.docx", SaveFormat.Docx);
    
// Get string from stream
string xml = Encoding.UTF8.GetString(xmlStream.GetBuffer(), 0, (int)xmlStream.Length);

Console.WriteLine(xml);