XMLMapping.SetMapping(String, String, CustomXMLPart) 方法

定义

允许创建或更改的内容控件的 XML 映射。 如果 Microsoft Office Word将内容控件映射到文档的自定义 XML 数据存储中的自定义 XML 节点,则返回 True

public bool SetMapping (string XPath, string PrefixMapping = "", Microsoft.Office.Core.CustomXMLPart Source = default);
abstract member SetMapping : string * string * Microsoft.Office.Core.CustomXMLPart -> bool
Public Function SetMapping (XPath As String, Optional PrefixMapping As String = "", Optional Source As CustomXMLPart = null) As Boolean

参数

XPath
String

指定 XPath 字符串,该字符串表示内容控件所映射到的 XML 节点。 无效的 XPath 字符串将导致运行时错误。

PrefixMapping
String

指定查询 参数中 XPath 提供的表达式时要使用的前缀映射。 如果省略,Word 将使用当前文档中指定的自定义 XML 部件的前缀映射集。

Source
CustomXMLPart

指定要将内容控件映射到所需的自定义 XML 数据。 如果省略此参数,则 XPath 计算依据在当前文档中的所有自定义 XML,使用 XPath 解析的 XML 节点的第一个 CustomXMLPart 建立映射。

返回

布尔值

示例

以下示例插入自定义 XML 部件并为该自定义部件设置 XML,然后在文档开头插入两个内容控件,并将控件的内容映射为自定义部件中 XML 元素的内容。

<span class="label">Dim objRange As Range































































































































Dim objCustomPart As CustomXMLPart































































































































Dim objCustomControl As ContentControl































































































































































































































































Set objCustomPart = ActiveDocument.CustomXMLParts.Add































































































































objCustomPart.LoadXML ("&lt;books&gt;&lt;book&gt;&lt;author&gt;Matt Hink&lt;/author&gt;" &amp; _































































































































    "&lt;title&gt;Migration Paths of the Red Breasted Robin&lt;/title&gt;" &amp; _































































































































    "&lt;genre&gt;non-fiction&lt;/genre&gt;&lt;price&gt;29.95&lt;/price&gt;" &amp; _































































































































    "&lt;pub_date&gt;2/1/2007&lt;/pub_date&gt;&lt;abstract&gt;You see them in " &amp; _































































































































    "the spring outside your windows.  You hear their lovely " &amp; _































































































































    "songs wafting in the warm spring air.  Now follow the path " &amp; _































































































































    "of the red breasted robin as it migrates to warmer climes " &amp; _































































































































    "in the fall, and then back to your back yard in the spring." &amp; _































































































































    "&lt;/abstract&gt;&lt;/book&gt;&lt;/books&gt;")































































































































































































































































ActiveDocument.Range.InsertParagraphBefore































































































































Set objRange = ActiveDocument.Paragraphs(1).Range































































































































Set objCustomControl = ActiveDocument.ContentControls _































































































































    .Add(wdContentControlText, objRange)































































































































objCustomControl.XMLMapping.</span>
<span class="label">SetMapping</span>
<span class="label">_    "/books/book/title", , objCustomPartobjRange.InsertParagraphAfterSet objRange = ActiveDocument.Paragraphs(2).RangeSet objCustomControl = ActiveDocument.ContentControls _    .Add(wdContentControlText, objRange)objCustomControl.XMLMapping.</span>
<span class="label">SetMapping</span>
<span class="label">_    "/books/book/abstract", , objCustomPart</span>

注解

如果 XML 映射已经存在,Word 将替换现有 XML 映射,并以新映射的 XML 节点的内容替换内容控件的文本。 如果指定的 XPath 的计算结果不是指定的自定义 XML 部件中的 XML 节点,那么仍可以指定映射,并且将创建一个映射。 当指定的 XPath 的计算结果为指定的自定义 XML 部件中的 XML 节点时,此映射将自动链接。

SetMappingByNode(CustomXMLNode) 请参阅 方法。

注意:为富文本内容控件创建映射会导致运行时错误。

适用于