Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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/89.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# 要将HTML值分配给字符串变量吗_C#_Html - Fatal编程技术网

C# 要将HTML值分配给字符串变量吗

C# 要将HTML值分配给字符串变量吗,c#,html,C#,Html,我想将HTML代码段分配给字符串变量。 差不多- string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello' string div=“Hello”//它应该只返回“Hello” 请建议。如果您确定HTML不会在您想要获取的字符串之间更改,您只需在两个常量字符串之间执行一个子字符串,即可将字符串放入变量中

我想将HTML代码段分配给字符串变量。 差不多-

 string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello'
string div=“Hello”//它应该只返回“Hello”

请建议。

如果您确定HTML不会在您想要获取的字符串之间更改,您只需在两个常量字符串之间执行一个子字符串,即可将字符串放入变量中

        const string prefix = "<table>";
        const string suffix = "</table>";
        string s = prefix + "TEST" + suffix ;
        string s2 = s.Substring(prefix.Length, s.IndexOf(suffix, StringComparison.Ordinal) - prefix.Length);
const字符串前缀=”;
常量字符串后缀=”;
字符串s=前缀+测试+后缀;
字符串s2=s.Substring(前缀.Length,s.IndexOf(后缀,StringComparison.Ordinal)-前缀.Length);
以下是正则表达式的版本:

        const string prefix = "<table>";
        const string suffix = "</table>";
        string s = prefix + "TEST" + suffix;
        string s2 = Regex.Match(s, prefix + "(.*)" + suffix).Groups[1].Value;
const字符串前缀=”;
常量字符串后缀=”;
字符串s=前缀+测试+后缀;
字符串s2=Regex.Match(s,前缀+“(.*)”+后缀)。组[1]。值;
string div=“Hello”//它应该只返回“你好”
var doc=新的XmlDocument();
doc.LoadXml(div);
字符串文本=doc.InnerText;

您还需要这个Jquery版本吗?

首先会有人提出regex。那么这个人就会被谋杀。然后有人会发布一个链接到HtmlAgilityPack。我看不出这个问题吗?您似乎已成功地将HTML放入字符串中。你是如何使用字符串的?冒着被谋杀的危险,这与你从哪里得到HTML相似?如果HTML不是完美的XML,那么这可能会失败。
        string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello

        var doc = new XmlDocument();
        doc.LoadXml(div);
        string text = doc.InnerText;