0

Please suggest me any solution to deserialize following xml string to datatable using c#. I didn't any related solution. Thanks in advance.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<seatMap>
  <errorCode>0</errorCode>
    <xml>
      <Seat-Map> 
        <Lower-Deck>
          <column id="1"> 
            <row id="1"><seat number="1C" status="0" /> </row> 
            <row id="2"><seat number="2C" status="0" /> </row> 
            <row id="3"><seat number="3C" status="0" /> </row> 
            <row id="4"><seat number="4C" status="0" /> </row> 
            <row id="5"><seat number="5C" status="0" /> </row> 
            <row id="6"><seat number="6C" status="0" /> </row> 
            <row id="7"><seat number="7C" status="0" /> </row> 
            <row id="8"><seat number="8C" status="0" /> </row> 
            <row id="9"><seat number="9C" status="0" /> </row> 
            <row id="10"><seat number="10C" status="0" /> </row> 
          </column>
          <column id="2"> 
            <row id="1"><seat number="1B" status="0" /> </row> 
            <row id="2"><seat number="2B" status="0" /> </row> 
            <row id="3"><seat number="3B" status="0" /> </row> 
            <row id="4"><seat number="4B" status="0" /> </row> 
            <row id="5"><seat number="5B" status="0" /> </row> 
            <row id="6"><seat number="6B" status="0" /> </row> 
            <row id="7"><seat number="7B" status="0" /> </row> 
            <row id="8"><seat number="8B" status="0" /> </row> 
            <row id="9"><seat number="9B" status="0" /> </row> 
            <row id="10"><seat number="10B" status="0" /> </row> 
          </column>
          <column id="3"> 
            <row id="1"><seat number="#" status="blank"/> </row> 
            <row id="2"><seat number="#" status="blank"/> </row> 
            <row id="3"><seat number="#" status="blank"/> </row> 
            <row id="4"><seat number="#" status="blank"/> </row> 
            <row id="5"><seat number="#" status="blank"/> </row> 
            <row id="6"><seat number="#" status="blank"/> </row> 
            <row id="7"><seat number="#" status="blank"/> </row> 
            <row id="8"><seat number="#" status="blank"/> </row> 
            <row id="9"><seat number="#" status="blank"/> </row> 
            <row id="10"><seat number="#" status="blank"/> </row> 
          </column>
          <column id="4"> 
             <row id="1"><seat number="1A" status="0" /> </row> 
             <row id="2"><seat number="2A" status="0" /> </row> 
             <row id="3"><seat number="3A" status="0" /> </row> 
             <row id="4"><seat number="4A" status="0" /> </row> 
             <row id="5"><seat number="5A" status="0" /> </row> 
             <row id="6"><seat number="6A" status="0" /> </row> 
             <row id="7"><seat number="7A" status="0" /> </row> 
             <row id="8"><seat number="8A" status="0" /> </row> 
             <row id="9"><seat number="9A" status="0" /> </row> 
             <row id="10"><seat number="10A" status="0" /> </row> 
          </column> 
        </Lower-Deck>
      </Seat-Map> 
    </xml>
  </seatMap>

But I need following DataTable from that XML String :

public static DataTable querySeat(string bus_id)
    {
        // Query the Seat Layout of the Trip
        DataTable dt = new DataTable();
        DataColumn dc;
        dc = new DataColumn("column", Type.GetType("System.Int32")); // Column No. of the seat
        dt.Columns.Add(dc);
        dc = new DataColumn("row", Type.GetType("System.Int32")); // Row N. of the seat
        dt.Columns.Add(dc);
        dt.Columns.Add("seat"); // Seat Label
        dc = new DataColumn("deck", Type.GetType("System.Int32")); // Deck, 1 = Lower Deck, 2 = Upper Deck
        dt.Columns.Add(dc);
        dt.Columns.Add("status"); // Status, 1 = Available, 0 = Taken
        return dt;
    }

DataTable Output :

Coulmn        |      Row      |       Seat     |      Deck     |   Status
--------------------------------------------------------------------------
1                    1                 1c               1           0
1                    2                 2c               1           0
1                    3                 3c               1           0 
CnuVas
  • 141
  • 2
  • 14

3 Answers3

0

Use the ReadXml method of the DataTable object.

Resource
  • 524
  • 4
  • 16
  • When I am using ReadXml method I am getting following output `code` errorCode | xml ------------------------------------------------------------------ 0 | ........ – CnuVas Nov 28 '14 at 09:03
  • You will probably have to spend some time working on the xml to get it right. Are you closing the lower-deck tag? – Resource Nov 28 '14 at 10:16
0

You can convert a DataSet to XML and back using the WriteXML and ReadXML methods of the Dataset. (check, for example, Convert Dataset to XML)

This is only possible if you also create the XML yourself from the application.

Community
  • 1
  • 1
Adrian Nasui
  • 1,054
  • 9
  • 10
0

Check the following Code

public static DataTable querySeat(string id)
{
    string xmlStr = "";           //Place Your XML String here

    DataTable result = new DataTable("SeatData");

    DataColumn columnColumn = result.Columns.Add("column", typeof(Int32));
    DataColumn rowColumn = result.Columns.Add("row", typeof(Int32));
    DataColumn seatColumn = result.Columns.Add("seat", typeof(String));
    DataColumn deckColumn = result.Columns.Add("deck", typeof(Int32));
    DataColumn statusColumn = result.Columns.Add("status", typeof(Int32));

    XElement.Parse(HttpUtility.HtmlDecode(xmlStr)).Descendants("seat").ToList().ForEach(
       (element) =>
          {
              DataRow row = result.NewRow();
              row.SetField<int>(columnColumn, Convert.ToInt32(element.Parent.Parent.Attribute("id").Value));
              row.SetField<int>(rowColumn, Convert.ToInt32(element.Parent.Attribute("id").Value));
              row.SetField<string>(seatColumn, element.Attribute("number").Value);
              int deckNo = -1;
              if (element.Parent.Parent.Parent.Name.LocalName == "Lower-Deck")
                 deckNo = 1;
              row.SetField<int>(deckColumn, deckNo);
              string statusString = element.Attribute("status").Value;
              int statusValue = -1;
              if (statusString == "blank")
                  statusValue = 1;
              else if (statusString == "0")
                  statusValue = 0;
              row.SetField<int>(statusColumn, statusValue);
              result.Rows.Add(row);
          }
       );
    return result;
}
CnuVas
  • 141
  • 2
  • 14