我正在使用这样的代码:
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);
}
最终发生的是我要添加的每个新字段,我必须重复这个冗余代码:
if (contentControl.Tag == "FieldName2")
{
string xPathFieldName2 = "ns:records/ns:record/ns:FieldName2";
contentControl.XMLMapping.SetMapping(xPathFieldName2,
prefix, currentWordDocumentXMLPart);
}
有没有一种方法可以让我编写一次此代码并动态更新每个字段的“FieldName”部分?即有某种类型的循环,它将通过 xml 文件中的每个 xmlnode 递增(在这种情况下,它会将 xml 节点 FieldName 映射到带有 FieldName 标记的内容控件,然后将 xml 节点 FieldName2 映射到带有FieldName2 的标签