简体   繁体   中英

C# OpenXML: Only getting first hyperlink in file in all files except for the last one

I've been assigned with writing an application that can go into a directory, find files of a certain type, get any hyperlinks in the file and record them. So far, it's been going smooth. However, for some reason I I'm having an issue with only getting one of the two links in the file. Both links are to the same url. It just seems to ignore the second one of the first two files, but the third one is just fine.

        string pathtofolder = "C:\\Users\\Icmolreulf\\source\\repos\\FileSearch\\FileSearch\\powershelltesting";

        string[] files = System.IO.Directory.GetFiles(pathtofolder, "*.docx");
        Console.WriteLine(files.Length);

        for (int i = 0; i < files.Length; i++)
        {
            WordprocessingDocument word = WordprocessingDocument.Open(files[i], true);
            IEnumerable<HyperlinkRelationship>  link = from x in word.MainDocumentPart.HyperlinkRelationships where (x.RelationshipType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink") select x;
            foreach (HyperlinkRelationship l in link)
            {
                if (isValidURL(l.Uri.ToString()))
                {
                    Console.WriteLine(l.Uri.ToString());
                }
            }
        }
    static bool isValidURL(string uriName)
    {
        Uri uriResult;
        bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
            && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

        return result;
    }

I'm at a loss for what the issue could be. The url I expect to find is the same one for all the files: http://burymewithmymoney.com/ . I have ensured it is in all of them. I have also tried adding another.docx file to the directory, and it works, but it also ignores that file too. I'd really appreciate any help I can get.

The issue was with the files I was reading from. Code works fine. Sorry for taking up your time. Have a nice rest of your day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM