c# - Update Word (OpenXml) Content Control DataBinding programmatically - figure out XPath -
update: solved problem (with of code http://dbe.codeplex.com).
i have issue metadata xml file not being shown in word file. says [myfieldname]
rather example 3354
.
i figured out reason quick part not working databinding.
- if add second quick part in word , link same document property show metadata property fine.
- if add "working" databinding not working sdtblock (sdtproperties) - original control work well!
the following code fixes second field control has invalid databinding.
databinding validbinding = null; int count = 0; foreach (openxmlelement openxmlelement in _contentcontrols) { sdtproperties sdtproperties = openxmlelement.elements<sdtproperties>().firstordefault(); if (sdtproperties == null) { throw new exception("expected find sdt properties!"); } tag tag = sdtproperties.elements<tag>().firstordefault(); console.writeline("\nprocessing sdt: {0}.", tag.val); sdtcontentrun sdtcontent = openxmlelement.elements<sdtcontentrun>().single(); console.writeline("current value: " + sdtcontent.innertext); if (!sdtcontent.innertext.startswith("[")) { // working control console.writeline("found working control"); validbinding = sdtproperties.elements<databinding>().single(); } else { console.writeline("found not working control"); databinding databinding = sdtproperties.elements<databinding>().firstordefault(); if (databinding != null) databinding.remove(); databinding = new databinding(); //databinding.prefixmappings = validbinding.prefixmappings; databinding.xpath = validbinding.xpath; databinding.storeitemid = _customxmlpart.customxmlpropertiespart.datastoreitem.itemid; sdtproperties.append(databinding); } }
how create new working databinding without creating quick part in ms word , copying settings?
databinding databinding = new databinding(); //databinding.prefixmappings = validbinding.prefixmappings; //databinding.xpath = validbinding.xpath; databinding.storeitemid = _customxmlpart.customxmlpropertiespart.datastoreitem.itemid;
how xpath?
working databinding looks <w:databinding w:xpath="/ns0:properties[1]/documentmanagement[1]/ns6:myfieldname[1]" w:storeitemid="{45c2489e-0875-4e37-ae02-cfeafc2294dc}" />
- why there no prefixmapping? apparently not required.
- still uses ns6:myfieldname , wonder ns6 defined..?
why /ns6:myfieldname[1]
? how know not ns374
?
the ns#: entries references namespaces nodes belong (namespace prefixes). these assigned word @ run-time , unpredictable. means that, @ point in future, ns6 may not work.
therefore have namespace management @ run-time prefix given namespace. you'll need namespace literals in order this.
ns0, example, may one: xmlns="http://schemas.openxmlformats.org/officedocument/2006/custom-properties
if you've used namespace in custom xml in customxmlpart you're linking content controls, 1 assigned ns6.
Comments
Post a Comment