【发布时间】:2021-04-09 12:53:09
【问题描述】:
我在 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 的 ContentControl 的 Range 中获取普通的超链接文本而不是其语法值,就像上面的用例一样存储?不知道我在这里做错了什么。
【问题讨论】:
标签: c# utf-8 vsto openxml word-contentcontrol