C# 如何从字符串中获取指定类的所有内容?

C# 如何从字符串中获取指定类的所有内容?,c#,xaml,C#,Xaml,我正在尝试为我的论坛制作一个适用于windows phone的应用程序。 以下是我目前为止的函数(仅以xaml为例): 我的代码中没有错误,我得到了xaml 我想做的是得到论坛的所有类别名称。所有类别名称都有一个“类别名称”类 如何获取类别名称?我能从绳子上拿吗?我必须解析字符串还是什么?我认为这篇关于codeproject的文章将帮助您: 它使用一个名为Htmlagilitypack的库,您可以通过NuGet安装该库: 下面的例子可能适用于您的目的,但由于防火墙问题,我无法对其进行测试,但

我正在尝试为我的论坛制作一个适用于windows phone的应用程序。 以下是我目前为止的函数(仅以xaml为例):

我的代码中没有错误,我得到了xaml

我想做的是得到论坛的所有类别名称。所有类别名称都有一个“类别名称”类


如何获取类别名称?我能从绳子上拿吗?我必须解析字符串还是什么?

我认为这篇关于codeproject的文章将帮助您:

它使用一个名为Htmlagilitypack的库,您可以通过NuGet安装该库:

下面的例子可能适用于您的目的,但由于防火墙问题,我无法对其进行测试,但希望它可以工作,并有助于回答您的问题

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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用系统文本;
使用HtmlAgilityPack;
命名空间HtmlParserDemo
{
班级计划
{
//将URL更新到您尝试解析的页面
私有常量字符串Url=”http://www.bing.com/";
私有常量字符串标记名=“a”;
私有常量字符串ClassName=“forumtitle”;
静态void Main(字符串[]参数)
{
尝试
{
WriteLine(“从:{0}获取HTML”,Url);
foreach(GetCategories中的变量类别(Url、标记名、类名))
{
控制台写入线(类别);
}
}
捕获(异常)
{
while(异常!=null)
{
控制台写入线(例外);
exception=exception.InnerException;
}
}
最后
{
Console.WriteLine(“按任意键退出…”);
Console.ReadKey(true);
}
}
公共静态IEnumerable GetCategories(字符串url、字符串htmlTag、字符串className=“”)
{
var response=new HttpClient().GetByteArrayAsync(url).Result;
字符串源=Encoding.GetEncoding(“utf-8”).GetString(响应,0,响应.长度-1);
source=WebUtility.HtmlDecode(源代码);
var result=新的HtmlDocument();
result.LoadHtml(源代码);
返回result.DocumentNode.subjects()
.Where(node=>node.Name==htmlTag&&
(string.IsNullOrEmpty(className)| |(node.Attributes[“class]”)!=null&&
node.Attributes[“class”]。Value==className)))
.Select(node=>node.InnerText);
}
}
}

在发送请求后,您可以发布textXAML的值吗?它非常长且难看,因此我将向您展示这一部分:class=\“forumtitle\”>技术侧注:为什么要使用XAML从服务器发送数据?发送标记可能有道理,但为什么需要在客户端上进行解析呢?您发布的半行代码看起来根本不像XAML。。。你确定你发布了正确的值吗?它是HTML。很抱歉用词不当。
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);
        }
    }
}