I am using code like this:
public void BindControlsToCustomXmlPart()
{
wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
foreach (Word.ContentControl contentControl in wordApp.ActiveDocument.ContentControls)
{
if (contentControl.Tag == "FieldName")
{
string xPathFieldName = "ns:records/ns:record/ns:FieldName";
contentControl.XMLMapping.SetMapping(xPathFieldName,
prefix, currentWordDocumentXMLPart);
}
What ends up happening is every new field I want to add, I have to repeat this redundant code:
if (contentControl.Tag == "FieldName2")
{
string xPathFieldName2 = "ns:records/ns:record/ns:FieldName2";
contentControl.XMLMapping.SetMapping(xPathFieldName2,
prefix, currentWordDocumentXMLPart);
}
Is there a way that I can write this code once and have the "FieldName" portion get updated for each field dynamically? i.e. have some type of loop that would increment through each xmlnode in an xml file (in this case it would map the xml node FieldName to the content control with a tag of FieldName, and then map the xml node FieldName2 to the content control with a tag of FieldName2
A good start would be creating a function to transform your control and reuse that function multiple times as followed
You could then use it in the following fashion
Next step would be to have a list of names you want to use for fields and feed it to your algorythm using a for loop
Hope this helps