wordpress hit counter
Re: Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed. - WordprocessingML - Formats - OpenXML Developer

Re: Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed.

Formats

Discussions about working with different Open XML Formats

Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed.

  • rated by 0 users
  • This post has 3 Replies |
  • 1 Follower
  • Hi guys!
    When I'm trying to open  one simple docx via Open Xml SDK , I'm getting an error such as  Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed. While examining this ducument i found that it containes only one hyperlink. In word\_rels\document.xml.rels part i found such record:
    <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://localhost:(TCP%20port%20number) * Shelest" TargetMode="External"/>
    If i change Target attribute to Target="http://localhost:80 * Shelest"   for example,  Open Xml SDK will open this docx without any problems.
    So my question is - how can I open this document programmatically by using Open Xml?

    Thank's fo any thoughts!

  • This sounds similar to a problem that I addressed in a blog post. Take a look at Workaround for Bad Link Issue in OpenXML SDK and see if that works for you.

  • You'r right, bobm, the problem is the same.

    I read you'r article and it seams to be workaround. So for no other solutions I modified you'r code for .net 3.5 and also for more functionality. Thanks!

  • private List<StringProcessor.KeyValue> badUris = new List<StringProcessor.KeyValue>();

           protected void fixBadUris(string filePath)

           {

               try

               {

                   // first search for *.rels parts and store path

                   using (ZipFile zip = ZipFile.Read(filePath))

                   {

                       ICollection<ZipEntry> zipEntries = zip.Entries;

                       for (int i = zipEntries.Count-1; i >= 0; i--)

                       {

                           ZipEntry item = zip[i];

                           if (item.FileName.EndsWith(".xml.rels"))

                           {

                               MemoryStream stream = new MemoryStream();

                               item.Extract(stream);

                               stream.Position = 0;

                               XmlDocument doc = new XmlDocument();

                               doc.Load(stream);

                               bool changed = false;

                               foreach (XmlNode el in doc.DocumentElement.ChildNodes.OfType<XmlNode>()

                                   .Where(n => n.Attributes["TargetMode"] != null && n.Attributes["TargetMode"].Value == "External"

                                       && !Uri.IsWellFormedUriString(n.Attributes["Target"].Value, UriKind.Absolute)))

                               {

                                   StringProcessor.KeyValue badUriRecord = new StringProcessor.KeyValue();

                                   badUriRecord.key = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());

                                   badUriRecord.value = el.Attributes["Target"].Value;

                                   // applying temp Uri

                                   el.Attributes["Target"].Value = badUriRecord.key;

                                   badUris.Add(badUriRecord);

                                   changed = true;

                               }

                               if (changed)

                               {                                

                                   zip.UpdateEntry(item.FileName, doc.OuterXml);

                                   zip.Save();

                               }

                           }

                       }

                   }

               }

               catch { }

           }

           protected string getOriginalUri(string tempUri)

           {

               string originalUri = badUris.Where(a => a.key == tempUri).DefaultIfEmpty().First().value;

               if (string.IsNullOrEmpty(originalUri)) return tempUri;

               return originalUri;

           }

           protected void restoreBadUris(string filePath)

           {

               if (badUris.Count == null) return;

               using (ZipFile zip = ZipFile.Read(filePath))

               {

                   ICollection<ZipEntry> zipEntries = zip.Entries;

                   for (int i = zipEntries.Count - 1; i >= 0; i--)

                   {

                       ZipEntry item = zip[i];

                       if (item.FileName.EndsWith(".xml.rels"))

                       {

                           MemoryStream stream = new MemoryStream();

                           item.Extract(stream);

                           stream.Position = 0;

                           XmlDocument doc = new XmlDocument();

                           doc.Load(stream);

                           bool changed = false;

                           foreach (StringProcessor.KeyValue currentUriPair in badUris)

                           {

                               foreach (XmlNode currentUriRef in doc.DocumentElement.ChildNodes.OfType<XmlNode>()

                                   .Where(a => a.Attributes["Target"] != null && a.Attributes["Target"].Value == currentUriPair.key))

                               {// working with current temp uri record in relation part. Restoring values

                                   currentUriRef.Attributes["Target"].Value = currentUriPair.value;

                                   changed = true;

                               }

                               if (changed)

                               {

                                   zip.UpdateEntry(item.FileName, doc.OuterXml);

                                   zip.Save();

                               }

                           }

                       }

                   }

               }

           }

Page 1 of 1 (4 items)