0

I have html table which was generated dynamically:

 <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. 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:

        // 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.

1

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.