如何从 ContentControl 的 Range.WordOpenXML 的存储值中获取纯超链接文本,而不是其语法值?

问题描述

我在 Word 的 ContentControl 中有如下超链接

http://www.yahoo.com

我将它的值存储如下以供以后使用

var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(cc.Range.WordOpenXML));

当我如下再次解码并获取其文本内容时,

var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(encoded));
XDocument doc = XDocument.Parse(decoded);
string ccText = doc.Descendants(XName.Get("document","http://schemas.openxmlformats.org/wordprocessingml/2006/main")).FirstOrDefault().Value;

这样我得到的是 HYPERLINK "http://www.yahoo.com/" \o "Follow link" 而不是 http://www.yahoo.com,期望结果是 http://www.yahoo.com

电子邮件也是如此,获取 HYPERLINK "mailto:abc@xyz.com" abc@xyz.com 而不是 abc@xyz.com

如果我在上述方法中使用 cc.Range.WordOpenXML获取文本内容,而不是解码,那么我将获得正确的值 http://www.yahoo.com

当我将解码后的 XML 与编码前的 XML 进行比较时,似乎 XML 的超链接节点正在被修改,我认为这是导致此问题的根本原因。

编码前的原始 XML: 检索自 doc.Descendants(XName.Get("document","http://schemas.openxmlformats.org/wordprocessingml/2006/main"))

<w:hyperlink r:id="rId4" w:tooltip="Follow link" w:history="1"> 
    <w:r w:rsidRPr="00E862A6">  
      <w:rPr>   
        <w:rStyle w:val="Hyperlink" />  
      </w:rPr>  
      <w:t>http://www.yahoo.com</w:t>   
    </w:r>  
  </w:hyperlink>

解码后更改了 XML 结构:

<w:ins w:id="5" w:author="xxxxxx xxxxxx" w:date="2021-03-30T16:42:00Z">
    <w:r>
      <w:instrText xml:space="preserve"> HYPERLINK "http://www.yahoo.com/" \o "Follow link" </w:instrText>
    </w:r>
    <w:r>
      <w:fldChar w:fldCharType="separate" />
    </w:r>
    <w:r w:rsidRPr="00E862A6">
      <w:rPr>
        <w:rStyle w:val="Hyperlink" />
      </w:rPr>
      <w:t>http://www.yahoo.com</w:t>
    </w:r>
    <w:r>
      <w:rPr>
        <w:rStyle w:val="Hyperlink" />
      </w:rPr>
      <w:fldChar w:fldCharType="end" />
    </w:r>
  </w:ins>

有什么方法可以从 Word 的 ContentControlRange获取纯超链接文本而不是其语法值,就像上面的用例一样存储?不确定我在这里做错了什么。

解决方法

对于这个根本原因我没有得到任何解决方案,所以直到我有办法从没有 HYPERLINK 语法的范围中检索有效的必需文本,

不是最好的方法或完美的解决方案,但作为现在的解决方法,我从字符串中删除了 HYPERLINK \"\\o \"Follow link\",以便在找到它在字符串中的位置后只得到 http://www.yahoo.com/

期待实际解决方案。