2014-07-06 2 views
1

나는이 문제에 조금 놀랐다. 일부 텍스트 뒤에 이미지가있는 html이 있습니다. 하지만 html을 재정렬하여 이미지가 먼저 나오도록해야합니다. 이미지, h3 태그, 텍스트.HTML에서 이미지를 추출하고 제목 태그 앞에서 이동하는 방법은 무엇입니까?

편집 : 아래의 대구는 실제로 스타일 속성을 제거하지 않습니다. html 소스를 더 자세히 살펴볼 때까지 작동하고 있다고 생각했습니다. 그래서 나는 지금까지 내가 사용하여 페이지에있는 이미지의 스타일 속성을 제거하기 위해 HAP을 사용하여 관리해야

<p> 
<img alt="" src="../../../../images/PeterDoocy5.jpg" style="width: 608px; height: 316px;" /></p> 

주어진 스타일 속성을 제거 도움이 필요 :

<Extension()> Public Function RemoveStyleAttributes(input As String) 
     Dim cleint As New WebClient 

     Dim html As New HtmlDocument 
     html.LoadHtml(input) 

     Dim elementsWithStyleAttribute = html.DocumentNode.SelectNodes("//@img") 

     If elementsWithStyleAttribute IsNot Nothing Then 
      For Each element In elementsWithStyleAttribute 
       element.Attributes("style").Remove() 
      Next 
     End If 
     Return input 
    End Function 

을하지만, 나는 어떻게하면 이미지를 가져 와서 H3 태그 앞에 놓을지를 놓치지 마세요.

HTML :

<div class="col-md-6"> 
    <div class="item"> 
     <div class="content galleryItem"> 
     <h3> 
      DOJ court docs in Abu Khattallah case dispel Obama Admin narrative about the anti-Islam video        
     </h3> 
     <p> 
      <img alt="" class="img-responsive" src="../../../../images/AbuKhattala.jpg" /> 
     </p> 
     <p> 
      But it was an awful, disgusting video..... 
     </p> 
     </div> 
    </div> 
</div> 

지금 확장 방법 :

@ Html.Raw로 사용하려고
<Extension()> Public Function RemoveStyleAttributes(html As HtmlDocument) 


     Dim divs = html.DocumentNode.SelectNodes("//div[@class='content galleryItem']") 

     For Each div As HtmlNode In divs 
      'get <img> and remove its style attribute' 
      Dim img = div.SelectSingleNode("./p/img[@style]") 
      img.Attributes("style").Remove() 
      'remove <h3> and <p>text here</p>' 
      Dim h3 = div.SelectSingleNode("./h3") 
      h3.Remove() 
      Dim text = div.SelectSingleNode("./p[not(img)]") 
      text.Remove() 
      'add <h3> and <p>text here</p> to the parent again in desired order' 
      div.AppendChild(h3) 
      div.AppendChild(text) 
     Next 


     Return html.DocumentNode.OuterHtml.ToString 
    End Function 

(item.PostSummary.RemoveStyleAttributes) 당신은이 방법을 시도 할 수 있습니다

답변

0

:

<Extension()> Public Function RemoveStyleAttributes(input As String) 
    Dim cleint As New WebClient 
    Dim html As New HtmlDocument 
    html.LoadHtml(input) 

    For Each div As HtmlNode In divs 
     'get <img> and remove its style attribute' 
     Dim img = div.SelectSingleNode("./p/img[@style]") 
     img.Attributes("style").Remove() 
     'remove <h3> and <p>text here</p>' 
     Dim h3 = div.SelectSingleNode("./h3") 
     h3.Remove() 
     Dim text = div.SelectSingleNode("./p[not(img)]") 
     text.Remove() 
     'add <h3> and <p>text here</p> to the parent again in desired order' 
     div.AppendChild(h3) 
     div.AppendChild(text) 
    Next 
    Return html.DocumentNode.OuterHtml.ToString 
End Function 

출력 (형식. 이 질문에 게시로 지정된 입력 HTML) 는 :

<div class="col-md-6"> 
    <div class="item"> 
     <div class="content galleryItem"> 
     <p> 
      <img alt="" class="img-responsive" src="../../../../images/AbuKhatta 
       la.jpg"> 
     </p> 
     <h3> 
      DOJ court docs in Abu Khattallah case dispel Obama Admin narrative a 
      bout the anti-Islam video 
     </h3> 
     <p> 
      But it was an awful, disgusting video..... 
     </p> 
     </div> 
    </div> 
</div> 
+0

내가 확장 방법으로 그것을 시도하고 개체 참조가 개체의 인스턴스로 설정되지 않았습니다 얻을. 내가보기 엔 @ Html.Raw (item.PostSummary.RemoveStyleAttributes)를 시도했지만 작동하지 않았다. 확장 메서드로 사용하는 한 내가 누락 된 부분은 무엇입니까? –

+0

확장 메서드가 원시 처리되지 않은'input' 대신에'html.DocumentNode.OuterElement'를 반환해야합니다 ... – har07

+0

만약 내가 html.documentnode.outerhtml을 반환하면 intellisense를 통해 확장 메서드로 나타나지 않습니다. 확장 메서드에 대한 원래 응답에 코드를 게시했습니다. –

관련 문제