XMLMapping.SetMapping(String, String, CustomXMLPart) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
允许创建或更改的内容控件的 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 ("<books><book><author>Matt Hink</author>" & _
"<title>Migration Paths of the Red Breasted Robin</title>" & _
"<genre>non-fiction</genre><price>29.95</price>" & _
"<pub_date>2/1/2007</pub_date><abstract>You see them in " & _
"the spring outside your windows. You hear their lovely " & _
"songs wafting in the warm spring air. Now follow the path " & _
"of the red breasted robin as it migrates to warmer climes " & _
"in the fall, and then back to your back yard in the spring." & _
"</abstract></book></books>")
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) 请参阅 方法。
注意:为富文本内容控件创建映射会导致运行时错误。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈