2016-11-10 37 views
0

想從Word文檔使用OpenXML每個段落提取超鏈接地址字的超鏈接。如何找到OpenXML的

public static string GetAddressFromPara(Paragraph Paras) 
{ 
    IEnumerable<Hyperlink> hplk = Paras.Descendants<Hyperlink>(); 
    if (hplk != null) 
    { 
     foreach (Hyperlink hp in hplk) 
     { 
      //string address = ???????; 
     } 
    } 
} 

回答

0

我相信它應該是

foreach (Hyperlink hp in hplk) 
{ 
    hyperlinkText = new StringBuilder(); 
    foreach (Text text in hp.Descendants<Text>()) 
     hyperlinkText.Append(text.InnerText); 

    hyperlinkRelationshipId = hp.Id.Value; 


    ExternalRelationship hyperlinkRelationship = doc 
     .MainDocumentPart 
     .ExternalRelationships 
     .Single(c => c.Id == hyperlinkRelationshipId); 

    hyperlinkUri = new StringBuilder(hyperlinkRelationship.Uri.AbsoluteUri); 
} 
+0

感謝您的答覆。獲取錯誤「序列包含不匹配的元素」上線ExternalRelationship hyperlinkRelationship = DOC ..... –

+0

遇到了這樣的回答搜索如何使用OPENXML和C#在字中的超鏈接獲取鏈接URL。這是HyperlinkRelationships而不是ExternalRelationships。直到我改變它,我纔得到序列錯誤。 HIH – Lysis