Search code examples
c#openxml-sdk

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.


Solution

  • 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.