none
Create Word document using values from XML file via C# program RRS feed

  • Question

  • I have precreated Word file.

    I have XML file.

    I would like to write C# program to read values from XML file and insert them into precreated Word file.

    What is recommended library for C# for inserting values from XML file into precreated Word file?

    Thanks.

    • Moved by CoolDadTx Thursday, March 23, 2017 2:14 PM Office related
    Thursday, March 23, 2017 3:05 AM

All replies

  • Hi,

    I would like to suggest you to use Open XML SDK to operate your word document.

    And there is a sample on GitHub: How to: Open and add text to a word processing document (Open XML SDK)

    For the part reading your xml file, you might take a look at the following posts:

    XML PARSING USING LINQ

    Reading XML using LINQ

    LINQ To XML Tutorials with Examples

    Hope this will be helpful to you.


    Ouch Liu , Developer ≈ Designer

    Thursday, March 23, 2017 4:50 AM
  • Hi pty1169,

    You can take a look at https://www.nuget.org/packages/FreeSpire.Doc/, I suppose it can achieve your purpose.

    Check below example, in which I read data from an XML file and then insert the data to a table in word document.

    DataSet ds = new DataSet(); ds.ReadXml("Orders.xml"); //get the first table DataTable dt = ds.Tables[0];

    //Load the Word document Document doc = new Document(); doc.LoadFromFile("Input.docx"); Section s = doc.AddSection(); Table tb = s.AddTable(true); tb.ResetCells(dt.Rows.Count + 1, dt.Columns.Count); string value = null; TableCell cell = null; TextRange range = null; //get the title row for (int i = 0; i < dt.Columns.Count; i++) { cell = tb.Rows[0].Cells[i]; cell.CellFormat.Borders.LineWidth = 1; cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = cell.AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TextRange txtRange = p.AppendText(dt.Columns[i].ColumnName); } //traverse the dt for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { value = dt.Rows[i][j].ToString(); cell = tb.Rows[i + 1].Cells[j]; cell.CellFormat.Borders.LineWidth = 1; cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p = cell.AddParagraph(); p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; range = p.AppendText(value); } } doc.SaveToFile("Output.docx", FileFormat.Docx);

    Hope this can help you.



    Friday, March 24, 2017 3:40 AM
  • Hi pty1169,
    I think Microsoft.Office.Interop.Word could be useful too.
    Here is an example:

      //Add reference Microsoft.Office.Interop.Word
                using Word = Microsoft.Office.Interop.Word;
    
                //get value from xml file
                XmlDocument xml = new XmlDocument();
                string filePath = @"C:\Users\Administrator\Desktop\1.xml";
                xml.Load(filePath);
                XmlNode node = xml.SelectSingleNode("Data/TestNode");
                string str = node.InnerText;
                //open a precreated word file
                Word._Application word = new Microsoft.Office.Interop.Word.Application();
                string docpath = @"C:\Users\Administrator\Desktop\Test.docx";
                Word.Document doc = word.Documents.Open(docpath);
                //insert value to word file
                Word.Range rng = doc.Range(0, 0);
                rng.Text = "New Text";
                //quit word application
                word.Quit(Word.WdSaveOptions.wdSaveChanges, Word.WdOriginalFormat.wdOriginalDocumentFormat);
    You could get more detail about inserting text into document from below link
    How to: Programmatically Insert Text into Word Documents
    Best Regards,
    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 24, 2017 8:03 AM
    Moderator
  • Although it is possible to manipulate word documents in C# using Office COM you may expeirance problems if you will want to run your application on the server. Office interop reqiures that the Office is installed on the machine where it runs. Microsoft itself advises developers to avoid that (see https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office).

    As an alternative you could use Docentric Toolkit. Our tests showed it has very good pdf conversion fidelity. It uses template based approach, where template documents are crated in MS Word, with placeholders for data. At runtime the template is merged with data to create final document. Data can be XML or any .NET data object.
    It's use is very simple. Here is the snippet for creating a final document from the template and XML data:

    XElement customers = XElement.Load("Customers.xml"); 
    DocumentGenerator dg = new DocumentGenerator(customers);
    DocumentGenerationResult result = dg.GenerateDocument("MyTemplate.docx", "MyReport.docx");

    Wednesday, May 3, 2017 7:13 PM