2016-08-23 1 views
-1

내 포럼 용 Windows Phone 용 앱을 만들려고합니다. 다음은 지금까지 (바로 XAML을 복용) 내 기능은 다음과 같습니다 내 코드에 오류가 없습니다문자열에서 지정된 클래스의 모든 내용을 가져 오는 방법은 무엇입니까?

private async void go_Click(object sender, RoutedEventArgs e) 
    { 
     HttpClient wc = new HttpClient(); 
     HttpResponseMessage response = await wc.GetAsync("http://www.myforum.com/"); 
     response.EnsureSuccessStatusCode(); 
     string xaml = await response.Content.ReadAsStringAsync(); 
     textXAML.Text = json; 
    } 

와 나는 XAML을 얻을.

내가 원하는 것은 포럼의 모든 카테고리 이름을 얻는 것입니다. 모든 카테고리 이름에는 "category-name"클래스가 있습니다.

카테고리 이름은 어떻게 얻을 수 있습니까? 끈에서 그들을 얻을 수 있습니까? 문자열이나 다른 것을 구문 분석해야합니까?

+0

요청을 제출 한 후에 textXAML 값을 게시 할 수 있습니까? –

+0

정말 길고 못 생겨서이 부분을 알려 드리겠습니다. class = \ "forumtitle \"> 기술 –

+0

참고 : XAML을 사용하여 서버에서 데이터를 보내는 이유는 무엇입니까? 마크 업을 보내는 것이 의미가있을 수 있지만 클라이언트에서 구문 분석해야하는 이유는 무엇입니까? –

답변

0

나는 CodeProject의에이 문서가 당신을 도울 것입니다 생각 :

http://www.nuget.org/packages/HtmlAgilityPack

예 아래 일 수

http://www.codeproject.com/Tips/804660/How-to-Parse-Html-using-csharp

그것은 당신이 NuGet을 통해 설치할 수 있습니다 Htmlagilitypack라는 라이브러리를 사용 귀하의 목적을 위해하지만 방화벽 문제로 인해 나는 그것을 테스트하지 못했지만 잘하면 그것이 작동하고 귀하의 질문에 대답하는 데 도움이됩니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Text; 
using HtmlAgilityPack; 

namespace HtmlParserDemo 
{ 
    class Program 
    { 
     // Update the URL to the page you are trying to parse 
     private const string Url = "http://www.bing.com/"; 
     private const string TagName = "a"; 
     private const string ClassName = "forumtitle"; 

     static void Main(string[] args) 
     { 
      try 
      { 
       Console.WriteLine("Getting HTML from: {0}", Url); 

       foreach (var category in GetCategories(Url, TagName, ClassName)) 
       { 
        Console.WriteLine(category); 
       } 
      } 
      catch (Exception exception) 
      { 
       while (exception != null) 
       { 
        Console.WriteLine(exception); 
        exception = exception.InnerException; 
       } 
      } 
      finally 
      { 
       Console.WriteLine("Press any key to exit..."); 
       Console.ReadKey(true); 
      } 
     } 

     public static IEnumerable<string> GetCategories(string url, string htmlTag, string className = "") 
     { 
      var response = new HttpClient().GetByteArrayAsync(url).Result; 
      string source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1); 
      source = WebUtility.HtmlDecode(source); 
      var result = new HtmlDocument(); 
      result.LoadHtml(source); 

      return result.DocumentNode.Descendants() 
       .Where(node => node.Name == htmlTag && 
           (string.IsNullOrEmpty(className) || (node.Attributes["class"] != null && 
           node.Attributes["class"].Value == className))) 
       .Select(node => node.InnerText); 
     } 
    } 
} 
관련 문제