Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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/76.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_Asp.net_Regex - Fatal编程技术网

C# 从大型html文件中获取元素

C# 从大型html文件中获取元素,c#,html,asp.net,regex,C#,Html,Asp.net,Regex,我有一个大的html文件(80 mo),如: ... ... 你好 ... ... ... ... ... ... ... ... ... ... ... 我不能手动修改这个html文件,所以最好是它保持只读 我想存储每行,以便以后能够对其进行操作。在这个div中,还有其他元素可以是任何东西 我尝试使用HtmlDocument和XmlDocument加载此文件,但该文件太大,导致内存不足异常 我试图使用正则表达式来获取表中的所有元素,但我无法管理它 我使用的正则表达式是: R

我有一个大的html文件(80 mo),如:


...
...
你好
... 
... 
... 
...
...
...
... 
... 
... 
... 
... 
我不能手动修改这个html文件,所以最好是它保持只读

我想存储每行
,以便以后能够对其进行操作。在这个div中,还有其他元素可以是任何东西

  • 我尝试使用HtmlDocument和XmlDocument加载此文件,但该文件太大,导致内存不足异常
  • 我试图使用正则表达式来获取表中的所有元素,但我无法管理它
  • 我使用的正则表达式是:

    Regex.Matches(myHtml, "<div class=\"phone\">[\\p{L}\\s]*\\,*[\\p{L}\\s]*<div");
    

    Regex.Matches(myHtml,“[\\p{L}\\s]*\\,*[\\p{L}\\s]*您可以使用jQuery在类
    phone
    中循环所有元素,并将它们存储在
    HiddenField
    中。然后在回发时,您可以访问这些值并处理它们

    <asp:HiddenField ID="HiddenField1" runat="server" />
    
    <script type="text/javascript">
        function getValues() {
            var valueArray = new Array();
            var valueString = "";
            $(".phone").each(function (index, element) {
                //for demo store both in hiddenfield and javascript array
                valueArray.push(element.innerHTML);
                valueString += element.innerHTML + ",";
            });
            $("#<%=HiddenField1.ClientID %>").val(valueString);
        }
    </script>
    

    您可以使用
    XmlReader
    类来读取文件。
    XmlReader
    不会将整个文件加载到内存中,但允许您在动态解析文档时逐点移动XML文档

    有关如何使用class=phone读取所有div内容的示例:

    using (XmlReader reader = XmlReader.Create(@"C:\A.html"))
    {
         // Loop over all xml tags 
         while (reader.Read())
         {
              // Check we have a div whith attribute class = phone
              if(reader.Name == "div" && reader.GetAttribute("class") == "phone")
              {
                   // Yes, so read until the corresponding closing tag and output content
                   textBox1.AppendText(reader.ReadInnerXml() + Environment.NewLine);
              }
         }
    }
    

    有关更多详细信息,请参阅。

    您是否尝试过使用仅转发
    XmlReader
    XPathNavigator
    方法?XPathNavigator获得了我的投票;它应该具有最低的内存占用,因为您正在查询文件,而不是将整个内容加载到内存中。我发现XPathNavigator有问题。类似于“十六进制值0x0C是一个无效字符“。要解决它,需要使用Regex.Replace…但我得到了内存不足异常…(),因此,我使用了XmlReader,它正在工作:)谢谢!这段代码的问题是ReadInnerXml()与Read()具有相同的函数类型。两者都移动到下一个标记。例如,ReadInnerXml出现时,指针将移动到下一个标记,然后当我们到达条件时,指针将再次跳到下一个标记…因此它忽略了1个标记…如果您不介意的话,我编辑了您的代码。感谢您的帮助。仍然需要改进,ReadInnerXml相当于调用Read.ReadInnerXml一直读取到结束标记,并将读取器的指针更改为下一个标记再次更改读取器指向下一个标记的指针。因此,同时跳过一个标记…跳过此div…这不是真的。ReadInnerXml读取到当前标记的结尾。这是真的。但是只有对Read的调用才会使下一个标记成为当前标记。因此,当前代码版本不会丢失任何标记。
    <asp:HiddenField ID="HiddenField1" runat="server" />
    
    <script type="text/javascript">
        function getValues() {
            var valueArray = new Array();
            var valueString = "";
            $(".phone").each(function (index, element) {
                //for demo store both in hiddenfield and javascript array
                valueArray.push(element.innerHTML);
                valueString += element.innerHTML + ",";
            });
            $("#<%=HiddenField1.ClientID %>").val(valueString);
        }
    </script>
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            string valueString = HiddenField1.Value;
            if (!string.IsNullOrEmpty(valueString))
            {
                string [] valueArray = valueString.TrimEnd(',').Split(',');
                foreach (string s in valueArray)
                {
                    //do stuff
                }
            }
        }
    
    using (XmlReader reader = XmlReader.Create(@"C:\A.html"))
    {
         // Loop over all xml tags 
         while (reader.Read())
         {
              // Check we have a div whith attribute class = phone
              if(reader.Name == "div" && reader.GetAttribute("class") == "phone")
              {
                   // Yes, so read until the corresponding closing tag and output content
                   textBox1.AppendText(reader.ReadInnerXml() + Environment.NewLine);
              }
         }
    }