how to convert word doc to xml and store in database

C

chandu

Hello,

i need to convert a word doc into xml format and need to store that xml doc
in to a sqlserver database..
how to convert a word doc to xml and after retrieving that xml doc from
database table how to back to word form and place it in a doc..

thanks a lot..
 
G

Guest

Changu,
From versions 2003 on, Word offers an option to Save As XML and will also
reload documents saved in this format.

If you are using the Word object model (requires a COM tab reference) in
your C# code, you can do this programmatically.

Storing in the database you would do as you would store any string of data.


Peter
 
J

joachim

Hi there
i need to convert a word doc into xml format
MS Word has its own conversion to XML. I don't think it's possible to
write your own filter for
Word's proprietary format. So there's only one possibility, and that is
to use Word automation:
prepare for bugs and untold syntax secrets.

Here's some code that might help you - but I warn you, this whole thing
never
actually made it to my desktop ...

// Don't forget to add a reference to the Interop dll
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern int ShowWindowAsync(IntPtr hWnd, int swCommand);

private void SaveAsXML(string filename)
{
Word.Application wordApp = new Word.Application();
// For reasons of performace and to avoid the user
// messing things up :
wordApp.Visible = false;
wordApp.ScreenUpdating = false;

// Word needs a lot of parameters that are optional in VB
// but need to be passed in C#
Object xmlFormat = Word.WdSaveFormat.wdFormatXML;
Object f = fileName;

wordApp.Documents.Open(
ref f,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing,
ref missing);

// Here comes the actual saving part
Word.Document doc = wordApp.ActiveDocument;
doc.SaveAs(
ref f,
ref xmlFormat,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing,
ref missing);

doc.Close(
ref missing,
ref missing,
ref missing);

CleanUpWordResources();
}

private void CleanUpWordresources()
{
// BUG: the COM proxy's reference (wordApp) seems to remain available
after
// the Word application has been closed. In order to avoid wordApp !=
null
// to throw an error it is safer to first look for running processes
// on OS level
processWord = Process.GetProcessesByName(PROCESS_WORD);
if (processWord.Length != 0)
{
if (wordApp != null)
{
//// Bring Application to front and close any open
WORD windows ////
foreach (Process p in processWord)
{
ShowWindowAsync(p.MainWindowHandle, (int)
ShowWindowConstants.SW_SHOWMAXIMIZED);
}


SetForegroundWindow(processWord[0].MainWindowHandle);

wordApp.Quit(ref missing, ref missing, ref
missing);
wordApp = null;
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}

private enum ShowWindowConstants : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}

There's some redundant code that brings the Word App (should you decide
to make it visible
with its Visible propert to true) to the foreground and closes it (to
notify the user that somethings is happening). But I thought I you
might need it once you start messing around with Office automation.

Storing the xml into a database shouldn't be too difficult

Success!
Joachim Van den Bogaert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to unlink TOC from a doc 0
XML to Dictionary using LINQ 2
XML doc creation via serialization 2
XML memory stream 8
WebService -> XML 3
Xpath c# 1
Edit XML document 4
Open XML doc from MemStream 9

Top