“超链接中的嵌入式URI无效”打开word文档C#时 [英] "Invalid embedded URI in hyperlink" when opening a word document C#

查看:99
本文介绍了“超链接中的嵌入式URI无效”打开word文档C#时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个使用WordprocessingDocument.Open(document,bool)打开word文档的应用程序。实现是正确的,因为它已经使用几个word doc文件工作了一段时间。由于word文档中的
电子邮件超链接无效,我遇到了文件无法打开的问题 在"@"处停止符号(" emailstuff @")。应用程序在数百甚至数千个word文档中搜索字符串,因此我希望处理该文件并且
只是跳过或忽略所有超链接以防止此问题导致文件失败,因为超链接永远不会已触及应用程序。 是否有现成的解决方法?

I have an application that opens a word document using WordprocessingDocument.Open(document,bool). The implementation is correct as it's been working for a while with several word doc files. I ran across an issue where the file fails to open due to an invalid email hyperlink inside the word document  that stops at the "@" symbol("emailstuff@"). The application searches for strings within hundreds and sometimes thousands of word documents, so I would like for the file to be processed and simply skip over or ignore all hyperlinks to prevent this issue from failing the file since the hyperlinks will never be touched with the application. Is there an existing workaround for this?

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))

谢谢

推荐答案

您好
E_oregel,

我试图找到问题,我得到的信息是这是OpenXML中的一个已知问题。

I try to find the issue and I get the information that this is a known issue in OpenXML.

许多其他用户已经面临同样的问题过去的问题。

many other users already face the same kind of issue in past.

好的事情是Eric White为这个问题提供了一些解决方法。

good thing is Eric White provided some work around for the issue.

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

class Program
{
    static void Main(string[] args)
    {
        var fileName = @"..\..\..\Test.docx";
        var newFileName = @"..\..\..\Fixed.docx";
        var newFileInfo = new FileInfo(newFileName);

        if (newFileInfo.Exists)
            newFileInfo.Delete();

        File.Copy(fileName, newFileName);

        WordprocessingDocument wDoc;
        try
        {
            using (wDoc = WordprocessingDocument.Open(newFileName, true))
            {
                ProcessDocument(wDoc);
            }
        }
        catch (OpenXmlPackageException e)
        {
            if (e.ToString().Contains("Invalid Hyperlink"))
            {
                using (FileStream fs = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
                }
                using (wDoc = WordprocessingDocument.Open(newFileName, true))
                {
                    ProcessDocument(wDoc);
                }
            }
        }
    }

    private static Uri FixUri(string brokenUri)
    {
        return new Uri("http://broken-link/");
    }

    private static void ProcessDocument(WordprocessingDocument wDoc)
    {
        var elementCount = wDoc.MainDocumentPart.Document.Descendants().Count();
        Console.WriteLine(elementCount);
    }
}

public static class UriFixer
{
    public static void FixInvalidUri(Stream fs, Func<string, Uri> invalidUriHandler)
    {
        XNamespace relNs = "http://schemas.openxmlformats.org/package/2006/relationships";
        using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Update))
        {
            foreach (var entry in za.Entries.ToList())
            {
                if (!entry.Name.EndsWith(".rels"))
                    continue;
                bool replaceEntry = false;
                XDocument entryXDoc = null;
                using (var entryStream = entry.Open())
                {
                    try
                    {
                        entryXDoc = XDocument.Load(entryStream);
                        if (entryXDoc.Root != null && entryXDoc.Root.Name.Namespace == relNs)
                        {
                            var urisToCheck = entryXDoc
                                .Descendants(relNs + "Relationship")
                                .Where(r => r.Attribute("TargetMode") != null && (string)r.Attribute("TargetMode") == "External");
                            foreach (var rel in urisToCheck)
                            {
                                var target = (string)rel.Attribute("Target");
                                if (target != null)
                                {
                                    try
                                    {
                                        Uri uri = new Uri(target);
                                    }
                                    catch (UriFormatException)
                                    {
                                        Uri newUri = invalidUriHandler(target);
                                        rel.Attribute("Target").Value = newUri.ToString();
                                        replaceEntry = true;
                                    }
                                }
                            }
                        }
                    }
                    catch (XmlException)
                    {
                        continue;
                    }
                }
                if (replaceEntry)
                {
                    var fullName = entry.FullName;
                    entry.Delete();
                    var newEntry = za.CreateEntry(fullName);
                    using (StreamWriter writer = new StreamWriter(newEntry.Open()))
                    using (XmlWriter xmlWriter = XmlWriter.Create(writer))
                    {
                        entryXDoc.WriteTo(xmlWriter);
                    }
                }
            }
        }
    }
}

请参阅以下链接以获取有关此问题的详细信息。

please refer link below to get detailed information on this issue.

在Open XML SDK中处理无效的超链接(OpenXmlPackageException)

格式错误的邮件到超链接会导致.NET 4.5+出现异常

免责声明:此回复包含对第三方万维网站点的引用。 Microsoft提供此信息是为了方便您。 Microsoft不控制这些网站
并且未测试在这些网站上找到的任何软件或信息;因此,Microsoft不能就其中发现的任何软件或信息的质量,安全性或适用性做出任何陈述。使用互联网上的任何
软件存在固有的危险,微软提醒您在从互联网上检索任何软件之前确保您完全了解风险。

Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

问候

Deepak


这篇关于“超链接中的嵌入式URI无效”打开word文档C#时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆