0

My customer gave me a webservice which was written in ColdFusion and I have been trying to consume it with C#. I have been trying to convert XML to DataTable but I am getting a DataTable with no rows.

Here is my XML:

<?xml version='1.0' standalone='yes'?>
<dtFirms>
  <NR>2</NR>
  <NAME>CS Net - 2014</NAME>
  <NRTIT>2 - 2014 - CS Net - 2014</NRTIT>
  <PERIOD>7</PERIOD>
</dtFirms>

and this is my C# code. It always return empty DataTable. What am I doing wrong?

    public DataTable ReadXmlIntoDataTable(string s) {

    //create the DataTable that will hold the data
    DataTable table = new DataTable("dtFirms");
    try
    {
        //open the file using a Stream
        using (Stream stream = GenerateStreamFromString(s))
        {
            //create the table with the appropriate column names
            table.Columns.Add("NR", typeof(string));
            table.Columns.Add("NAME", typeof(string));
            table.Columns.Add("NRTIT", typeof(string));
            table.Columns.Add("PERIOD", typeof(int));

            //use ReadXml to read the XML stream
            table.ReadXml(stream);

            //return the results
            return table;
        }
    }
    catch (Exception ex)
    {
        return table;
    }
}


public Stream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

It always return empty DataTable. What am I doing wrong? Please help me.

user353gre3
  • 2,747
  • 4
  • 24
  • 27
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

1 Answers1

0
  1. You can create schema for your xml file.

  2. By using XSD.exe you can generate class structure for schema. Use these xml schema classes for iterating through the xml file.

  3. Use table.Rows.Add() function to add entries in data table.

AkshayP
  • 188
  • 3
  • 11