0

I have this below XML string , got it from SqlDependency that monitor sql table ,I want to Convert it to List or datatable

<root>
  <inserted>
    <row>
      <invoice_id>26</invoice_id>
      <invoice_type>5</invoice_type>
      <client_id>372</client_id>
      <create_date>2019-12-02T13:49:21.430</create_date>
      <status_id>1</status_id>
      <sales_rep_id>1</sales_rep_id>
      <courrier_id>12</courrier_id>
      <items_quantity>3</items_quantity>
      <sub_total>0.00</sub_total>
      <discount>0.00</discount>
      <shipping>0.00</shipping>
      <tax>0.00</tax>
      <grand_total>0.00</grand_total>
      <prepared_by>11</prepared_by>
      <current_state>PREPARED</current_state>
      <store_ID>1</store_ID>
      <extra></extra>
      <user_id>11</user_id>
      <open_date>2019-12-02T13:52:53.583</open_date>
      <prepared_date>2019-12-02T13:54:10.877</prepared_date>
      <preparing_notes></preparing_notes>
      <to_courier_date>2019-12-02T14:34:17.953</to_courier_date>
    </row>
  </inserted>

</root> 

I tried the below code :

string xml = ee.Data.ToString();// XML string above
                List<string> Lst = new List<string>();
                XmlReader xr = XmlReader.Create(new StringReader(xml));
                var xMembers = from members in XElement.Load(xr).Elements() select members;

                foreach (XElement x in ee.Data.Elements("root"))
                {
                    Lst.Add (  x.Value);
                }

but not working also tired for converting it to Datatable

public DataTable XML2DT(string xmlData)
        {
            StringReader theReader = new StringReader(xmlData);
            DataSet theDataSet = new DataSet();
            theDataSet.ReadXml(theReader);

            return theDataSet.Tables[0];
        }

return error , so can anyone help me to convert it to any usable object to store there new data

Isaac Be
  • 81
  • 1
  • 10
  • 1
    Does this answer your question? [How can I transform XML into a List or String\[\]?](https://stackoverflow.com/questions/956749/how-can-i-transform-xml-into-a-liststring-or-string) – dvo Dec 04 '19 at 14:12

3 Answers3

0

Please try below code snippet.

string xmlFile = "Data.xml";
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);
Marwen Jaffel
  • 703
  • 1
  • 6
  • 14
0

Using xml linq you can put into a dictionary :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
         static void Main(string[] args)
         {
             XDocument doc = XDocument.Load(FILENAME);

             Dictionary<string, string> dict = doc.Descendants("row").FirstOrDefault().Elements()
                 .GroupBy(x => x.Name.LocalName, y => (string)y)
                 .ToDictionary(x => x.Key, y => y.FirstOrDefault());

         }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

If you define yourself classes:

public class root
{
    public row[] inserted {get;set;}
}

public class row
{
    public int invoice_id {get;set;}
    public int invoice_type {get;set;}
    .....
}

and you use Xml-Serialization

    var ser = new XmlSerializer(typeof(root));
    var root = (root)ser.Deserialize(new StringReader( yourxmlstring ));

than you have a type-safe conversion of all your xml.

I assume your row-Element maybe repeated (is a List), while the inserted-tag is unique.

It's not really a "List or Datatable" but still good for Databinding. Unless your list of properties changes daily and you don't known them in advance.

Holger
  • 2,446
  • 1
  • 14
  • 13
  • thank you this solve my issue `var ser = new XmlSerializer(typeof(root)); var Xroot = (root)ser.Deserialize(new StringReader(ee.Data.ToString())); List Lst = new List(); foreach (row r in Xroot.inserted) { Lst.Add (r.invoice_id.ToString()); }` – Isaac Be Dec 04 '19 at 19:48