Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:26:11+00:00 2026-06-11T13:26:11+00:00

I was able to create a Word Document with content controls mapped to an

  • 0

I was able to create a Word Document with content controls mapped to an Xml schema and using the code from this blog: http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/ I am able to insert data into the word document.

The question I have is, is it possible to replace the the code below so that I can use an Xml file instead of having to write this for each finding:

//create XML string matching schema custom XML path
            string newXml = "<root>" +
                "<FINDING>Adobe Flash Player contains multiple...</FINDING>" +
                "<STATUS>Open</STATUS>" +
                "<THREATLEVEL>High</THREATLEVEL>" +
                "<RECOMMENDATION>Update Flash Player to version...</RECOMMENDATION>" +
                "<DEVICEAFFECTED>UserPC</DEVICEAFFECTED>" +
                "<SCANNER>XXXXXX</SCANNER>" +
                "</root>";

I have tried replacing this with:
string newXml = @”C:\Users\Christopher\Desktop\BookData\TestReport.xml”;
and created a nested using statement with StreamReader and the existing StreamWriter but the word document would not populate and there would not be any errors.

–I just tried to replace that code with this:
//create XML string matching schema custom XML path
string newXml = @”C:\Users\Christopher\Desktop\BookData\TestReport.xml”;
using (StreamReader sr = new StreamReader (newXml))
{
newXml = sr.ReadToEnd();
}

and I no longer get the error when I open the document, but the content controls are not populating?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:26:12+00:00Added an answer on June 11, 2026 at 1:26 pm

    Turns out the problem I was running into was that my code and examples were not actually deleting the previous CustomXMLParts located in the “CustomXML” folder. I the code working in two ways, the first is a Windows Form App with one button (btnGenerate) which allows you to select both the template.docx and customXML.xml files. The second is the a Console Program.
    Cheers
    Windows Form Application:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Threading;
    
    
    namespace TestReportCreator_Beta
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        [STAThread]
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string outFile = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
    
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Multiselect = false;
            OFD.Title = "Open Word Document";
            OFD.Filter = "Word Document|*.docx;*.domx";
            OFD.ShowDialog();
            string docPath = OFD.FileName;
            OFD.Title = "Opne Xml Document";
            OFD.Filter = "Xml Document|*.xml";
            OFD.ShowDialog();
            string xmlPath = OFD.FileName;
    
            // convert template to document
            File.Copy(docPath, outFile);
    
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream fs = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            } 
        }
    }
    }
    

    Here is the Console Program Version

    using System; using System.Collections.Generic;
    using System.Linq; using System.Text;
    using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing; using System.IO;
    
    namespace BookData
    {
    class Program
    {
    
        static void Main(string[] args)
        {
            string template = @"C:\Users\Christopher\Desktop\BookData\TestReportBeta.docx";
            string outFile = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
            string xmlPath = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml";
    
            // convert template to document
            File.Copy(template, outFile);
    
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream fs = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            } 
        }
    }
    }
    

    I hope you fellow developers and office automation folks out there are able put this to good use!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am able to create a UIImage from a Core Animation layer using the
I want to create a word 2007 document without using object model. So I
The intranet website is supposed to create a word document client-side from templates server-side
I creating a C# application that has to create a word document. I'm using
I am trying to create the word document using the HWPFDocument . I am
I'm able to create html links the following way: <a href=/about>About Us</a> This will
I'm able to create an excel file using apache poi. however, i want users
I am able to create a connection to a local sqlite3 database ( Using
I am able to create alternate dictation grammars using the dictation resource kit or
I have some C# automation code which does interesting things with Microsoft.Office.Interop.Word.Document objects. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.