none
XML to MS Word Using C#.NET RRS feed

  • Question

  • Hi,

    I have a requirement where i need to use a XML to convert it to any Word(2003/2007 either doc or docx) format with some custom styles using XSLT. Anyone has suggestions or reference links to start this? Aprreciate your help.

    -Bhaskar.

    Friday, October 21, 2011 8:32 PM

All replies

  • HI

    you can use this

     

    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;
     
    namespace Xml2Pdf
    {
        class Program
        {
            static void Main(string[] args)
            {
                Document doc = new Document();
                doc.LoadFromFile("sample.xml", FileFormat.Xml);
                doc.SaveToFile("test.doc", FileFormat.Doc);
            }
        }
    }

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, October 22, 2011 1:13 AM
  • You need to download spire.Doc first

    http://www.e-iceblue.com/Download/download-word-for-net-now.html

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Edited by Lie You Tuesday, October 25, 2011 2:19 AM link
    Saturday, October 22, 2011 1:15 AM
  • Hi again

    you can use this it's great link

    http://stephenchy520.blog.com/2011/06/10/how-to-convert-xml-to-doc-with-cvb-net/

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Edited by Lie You Tuesday, October 25, 2011 2:19 AM link
    Saturday, October 22, 2011 1:17 AM
  • Thanks Salem for you replies.

     

    Actually, i am looking to achieve the below :

    1. I have a XML file say like below

    <Chapter Id="1">

        <Name>ABC </Name>

        <Number>1</Number>

         <Data>This is a test article </Data>

    .........

    ..........

    </Chapter>

    2. For every element, should map a style using XSLT, like for <Name> it should be Bold and italic, For <Data> it should be like paragraph

    3. Using these two, should generate .doc/.docx file.

     

    I found this article in Bing , which is useful but this is different way where it doesnt fit to my requirement.

    http://msdn.microsoft.com/en-us/library/ee872374(office.12).aspx

     

    Any help/pointers is really appreciated.

     

    -Bhaskar.

    Saturday, October 22, 2011 7:15 AM
  • Hi Bhaskar.

    I found someone says

    Dont worry....Converting XML file with XSL into word is so easy.....
    First you need to design your XML file by using Infopath...then after that Export into Web (Mht) format. Open the Mht format in Word.

    the post in

    http://news.oreilly.com/2008/08/importing-xml-documents-into-w.html

     

     

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, October 22, 2011 11:13 PM

  • Perhaps using WordML will work for your needs.  Here is a document that gives an overview: http://msdn.microsoft.com/en-us/library/aa212812(v=office.11).aspx.
     

    --
    Mike
    Sunday, October 23, 2011 12:25 AM
  • Hi Mike,

    Infact, i tried today with Open XML. But, got stucked at this point.

     

    1. I have all the custom style in a word 2007/2010 document.

    2. Changed the word extension from docx to .zip and extracted all the files.

    3. I am able to see the styles.xml, document.xml and other xml files.

     

    I am thinking to programatically map my XML with word (styles.xml), as mentioned in this link:

    http://www.codeproject.com/KB/office/OpenXML-SDK-HelloWorld.aspx?display=Print

    But, the above link is creating the styles using C#.net. But, i want to add my custom styles at runtime instead of C#.

     

    Problem:

    Let's say this is my one of the custom style took from styles.xml

    <w:style w:styleId="Documentnumber" w:type="paragraph" w:customStyle="1"><w:name w:val="Document_number"/><w:autoRedefine/><w:rsid w:val="009B48C1"/><w:pPr><w:spacing w:lineRule="auto" w:line="240" w:after="480"/></w:pPr><w:rPr><w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman" w:hAnsi="Arial Unicode MS" w:ascii="Arial Unicode MS"/><w:color w:val="8B271D"/><w:sz w:val="60"/><w:szCs w:val="20"/></w:rPr></w:style>

    If you see the codeproject.com example, i want to integrate the above style instead of "MyHeading1" in the example of codeproject.com

     

    This way, i thought i can create a mapping between my XML file and word styles.xml and create word file using some c# code.

     

    please let me know if the above can be achieved in any other way?

     

     

     

    Monday, October 24, 2011 9:09 AM
  • Hello Bhaskar_Basu,

    In your last post you say :

    But, the above link is creating the styles using C#.net. But, i want to add my custom styles at runtime instead of C#.”, and say “This way, i thought i can create a mapping between my XML file and word styles.xml and create word file using some c# code.”
     
    If you want your XML data to be rendered as a Word 2007 or Word 2010 document you will need to build it with the parts you see when you unpacked the test document. In that you see strings of text between <w:r…> … </w:r> tags. Associated with those runs you should see several 6-digit values that refer to styles, fonts, etc. Those refer to the specific elements of the styles.xml file you got by unpacking the original document. Any change in fonts, styles, or other attributes causes that run to get the end tag (</r>) and the next run, be it only one character, to get a new <w:r…>..</w:r> tags with the appropriate 6-digit reference strings.

    Then you ask “please let me know if the above can be achieved in any other way?” The inference is that you have some overriding reason for not using a transform. Why is that?

    Could you tell us more about the reasons for, and the meaning of “, i want to add my custom styles at runtime instead of C#”

    Please let us know more about your project. Thanks.

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    Thursday, October 27, 2011 9:49 PM
    Moderator
  • I am bothered with the same problem and think that if I can find a solution in this post but failed.

    Could someone give more suggestions?

    Friday, October 26, 2012 6:27 AM
  • Good one
    Friday, December 7, 2018 1:04 PM