【问题标题】:How to read data from HTML table starting from specified cell using C#?如何使用 C# 从指定单元格开始从 HTML 表中读取数据?
【发布时间】:2015-07-21 03:54:24
【问题描述】:

我有动态生成的 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>

我想将此类表的一部分导出到 XML 文件。所以我必须从第 3 行开始读取值,但我不知道该怎么做。

我使用以下代码创建 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);

如果有人可以帮助我解决这个问题,我会很高兴。欢迎任何想法。提前致谢。

【问题讨论】:

标签: c# xml html-table


【解决方案1】:

你做的很艰难。见下面的代码

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);
        }
    }
}
​

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2013-10-23
    相关资源
    最近更新 更多