Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Windows8中用c解析字符串?_C#_Html_Windows 8_Windows Store Apps_Winrt Xaml - Fatal编程技术网

C# 如何在Windows8中用c解析字符串?

C# 如何在Windows8中用c解析字符串?,c#,html,windows-8,windows-store-apps,winrt-xaml,C#,Html,Windows 8,Windows Store Apps,Winrt Xaml,我有一个包含这部分HTML代码的网页: <meta property="og:type" content="photo" /> <meta property="og:description" content="descrizione"> <meta property="og:site_name" content="Site_Name" /> <meta property="og:title" content="" />

我有一个包含这部分HTML代码的网页:

<meta property="og:type" content="photo" />
    <meta property="og:description" content="descrizione">

    <meta property="og:site_name" content="Site_Name" />
    <meta property="og:title" content="" />
    <meta property="og:image" content="http://addfsfdbyhdfsifd.jpg" />
    <meta property="og:determiner" content="a" />
    <meta property="fb:app_id" content="124024574287414" />
    <meta property="og:url" content="http://addfsfdbyhdfsifd.com" />
如何拾取属性=og:image的内容?我需要获得该链接才能在我的应用程序中显示它。

使用


设置HtmlDocument的代码:

var s = @"
<meta property=""og:type"" content=""photo"" />
<meta property=""og:description"" content=""descrizione"">

<meta property=""og:site_name"" content=""Site_Name"" />
<meta property=""og:title"" content="""" />
<meta property=""og:image"" content=""http://addfsfdbyhdfsifd.jpg"" />
<meta property=""og:determiner"" content=""a"" />
<meta property=""fb:app_id"" content=""124024574287414"" />
<meta property=""og:url"" content=""http://addfsfdbyhdfsifd.com"" />";

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(s);

这是一个成功的方法

我不喜欢用第三方库来解决一个小问题。顺便说一句,对敏捷背包没有冒犯。它超级强大。您永远不想自己解析HTML。但这是一个如此小的边缘案例!为什么要搞砸它? 你不能确定HTML是否会被解析,所以你需要一些别的东西。XML很诱人,但除非您100%肯定这是有效的XHTML,否则最好不要追求它。只需将其视为字符串解析练习。 那么什么最能解析字符串呢,就是这样

以下是您的解决方案:

var s = @"
<meta property=""og:type"" content=""photo"" />
<meta property=""og:description"" content=""descrizione"">
<meta property=""og:site_name"" content=""Site_Name"" />
<meta property=""og:title"" content="""" />
<meta property=""og:image"" content=""http://addfsfdbyhdfsifd.jpg"" />
<meta property=""og:determiner"" content=""a"" />
<meta property=""fb:app_id"" content=""124024574287414"" />
<meta property=""og:url"" content=""http://addfsfdbyhdfsifd.com"" />";

// first define what you will look for using regex pattern syntax
var p = @"meta\s{1,}property=""og:image""\s{1,}content=""(.+)""\s{0,}/>";

// second let the regex engine use your pattern against your html string
var m = System.Text.RegularExpressions.Regex.Match(s, p);

// third pull out just the part you want from the resulting match
var g = m.Groups[1];

// forth get the value from the meta tag, specifically the og:image you wanted
var i = g.Value;
是的,就这么简单。Regex也让它更可靠


祝你好运

你想用C来解析它吗?在服务器端?看一看用于解析C中Html的Html Agility pack应该是一个注释…即使是关闭的,标记最佳答案也是礼貌的。XPath?什么你为什么还在使用XPath?它是有效的,是的。它可读吗?没有。有更好的方法吗?是的。请随意提供一个更好、更可读的答案。我不同意;我喜欢xPath。优雅而有力。如果你知道的话,你可以读。如果你不知道,当然很难。嗯。Lambdas,Regex,F-你不知道的东西总是很有挑战性的。那个论点很愚蠢。话虽如此,大多数HTML文档不会解析为XmlDocument。如果有任何两个具有相同属性的元标记,则此代码将爆炸。@Shlomo是的,一个包含多个具有相同属性名称的元标记的HTML页面值得这样做。该页面值得这样做,该页面的开发人员也应该这样做。但是解析器的开发人员可能不会。@Shlomo那么OP可以简单地用ToLookup替换ToDictionary:
var node = doc.DocumentNode.SelectNodes("//meta[@property='og:image']").FirstOrDefault();

var content = node != null
    ? node.GetAttributeValue("content", string.Empty)
    : string.Empty;
var s = @"
<meta property=""og:type"" content=""photo"" />
<meta property=""og:description"" content=""descrizione"">
<meta property=""og:site_name"" content=""Site_Name"" />
<meta property=""og:title"" content="""" />
<meta property=""og:image"" content=""http://addfsfdbyhdfsifd.jpg"" />
<meta property=""og:determiner"" content=""a"" />
<meta property=""fb:app_id"" content=""124024574287414"" />
<meta property=""og:url"" content=""http://addfsfdbyhdfsifd.com"" />";

// first define what you will look for using regex pattern syntax
var p = @"meta\s{1,}property=""og:image""\s{1,}content=""(.+)""\s{0,}/>";

// second let the regex engine use your pattern against your html string
var m = System.Text.RegularExpressions.Regex.Match(s, p);

// third pull out just the part you want from the resulting match
var g = m.Groups[1];

// forth get the value from the meta tag, specifically the og:image you wanted
var i = g.Value;