简体   繁体   English

如何使用C#从指定单元格开始从HTML表读取数据?

[英]How to read data from HTML table starting from specified cell using C#?

I have html table which was generated dynamically: 我有动态生成的html表:

 <table id="ReportTab">               
            <tr >
                <td rowspan="2">header_1</td>
                <td colspan="2">header_2</td>
                <td colspan="3">header_3</td>
            </tr>
            <tr>
                <td>header_2.1</td>
                <td>header_2.2</td>
                <td>header_3.1</td>
                <td>header_3.2</td>
                <td>header_3.3</td>                
            </tr>
            <tr>
                <td>12653</td>
                <td>323</td>
                <td>87</td>
                <td>546</td>
                <td>346</td>
                <td>463</td>              
            </tr>
        </table>

I want to export a part of such table to XML file. 我想将此类表的一部分导出到XML文件。 So I have to start reading values from the 3rd row but I have no idea how to do it. 所以我必须从第三行开始读取值,但是我不知道该怎么做。

I use the following code to create XML file: 我使用以下代码创建XML文件:

        // create a new blank file
        XmlTextWriter textWriter = new XmlTextWriter(pathToXml, null);
        textWriter.WriteStartDocument();            
        textWriter.Close();

        // write data to the xml document
        XmlDocument document = new XmlDocument();
        document.Load(pathToXml);

        XmlNode element = document.CreateElement("region");
        document.DocumentElement.AppendChild(element);

        // read value from the table cell
        // I supporse this part of code should be inside the loop
        XmlNode subElement = document.CreateElement("value"); 
        subElement.InnerText = ""; // here I need to put value from the table
        element.AppendChild(subElement); 

        document.Save(pathToXml);

I would be glad if somebody could help me to solve this problem. 如果有人可以帮助我解决这个问题,我将感到非常高兴。 Any ideas are welcome. 任何想法都欢迎。 Thanks in advance. 提前致谢。

You are doing to the hard way. 您正在努力工作。 See code below 见下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string XMLdefinition = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            string input = 
                "<table id=\"ReportTab\">" +               
                   "<tr >" +
                       "<td rowspan=\"2\">header_1</td>" +
                       "<td colspan=\"2\">header_2</td>" +
                       "<td colspan=\"3\">header_3</td>" +
                   "</tr>" +
                   "<tr>" +
                       "<td>header_2.1</td>" +
                       "<td>header_2.2</td>" +
                       "<td>header_3.1</td>" +
                       "<td>header_3.2</td>" +
                       "<td>header_3.3</td>" +             
                   "</tr>" +
                   "<tr>" +
                       "<td>12653</td>" +
                       "<td>323</td>" +
                       "<td>87</td>" +
                       "<td>546</td>" +
                       "<td>346</td>" +
                       "<td>463</td>" +              
                   "</tr>" +
               "</table>";

            string XML = XMLdefinition + input;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(XML);
            doc.Save(FILENAME);
        }
    }
}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM