3

I have two strings. One string is having XML Data and another string is having corresponding XML Schema. I am trying to read the data in DataTable. It looks like it is not possible. I don't want to use dataset. Is there a way I can combine the XML data and Schema into a memory stream and read?

John Woo
  • 258,903
  • 69
  • 498
  • 492
Rajkumar Vasan
  • 712
  • 1
  • 9
  • 22
  • DaraTable/Dataset only understand their own schema, not one you bring along. Try it without the schema. – H H Jan 05 '12 at 09:19
  • It looks like DataSet is able to read the data without schema. But When i tried with datatable i got the following error. Datatable does not support schema inference from xml. – Rajkumar Vasan Jan 05 '12 at 09:21
  • "DataSet is able to read the data" - So, problem solved? – H H Jan 05 '12 at 09:24
  • @Henk, Yes the problem is solved. But I am not suppose to use the dataset. Otherwise I should be able to explain why datatable is not able to read XML Data. – Rajkumar Vasan Jan 05 '12 at 09:31
  • A XML file could contain more than one 'table' so it makes sense that the DataSet is the main class for parsing it. But why "not suppose to use the dataset" ?? Makes no sense. – H H Jan 05 '12 at 09:36
  • @Henk, Thanks I will use DataSet. At the sametime will try to findout how to use datatable also. – Rajkumar Vasan Jan 05 '12 at 09:38
  • `ds.Tables[0]` holds your table. You can forget about the ds after reading. – H H Jan 05 '12 at 09:41
  • May be if you can post schema and xml that will help – Surjit Samra Jan 05 '12 at 11:43

2 Answers2

4

Put simply, no, there is not a way to load xml directly into a DataTable through methods on DataTable nor is there a way to create a DataTable directly from an arbitrary schema. Such operations must be done through DataSet; otherwise, you end up doing some very involved workarounds.

There are some techniques you could apply using xml serialization that would be able to recreate a dataset from previously serialized xml. This does not allow for the use of an arbitrary schema though.

You could also write code specifically that loads your XML (via XDocument, XmlDocument, or XmlTextReader) and creates a DataTable on the fly, but it's not trivial to write and would likely take you quite some time. It's also kind of reinventing the wheel.

Essentially, the DataSet is the only class in that hierarchy with methods to process XML because Xml could contain any number of tables. To handle the broadest number of cases when you can make almost no assumptions about the XML, it has to be implemented at that level.

You could also consider whether it's appropriate to simply load the xml into an XDocument, validate it using the Validate extension method, and use Linq to Xml to query it.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
4

Is there a way I can combine the XML data and Schema into a memory stream and read?


Method

     static DataTable ParseXML(string xmlString)
     {
         DataSet ds = new DataSet();
         byte[] xmlBytes = Encoding.UTF8.GetBytes(xmlString);
         Stream memory = new MemoryStream(xmlBytes);
         ds.ReadXml(memory);
         return ds.Tables[0];
     }

Example:

            string xml = new XElement("inventory",
                new XElement("item",
                    new XElement("name", "rock"),
                    new XElement("price", "5000")),
                new XElement("item",
                    new XElement("name", "new car"),
                    new XElement("price", "1"))).ToString();

            DataTable dt = ParseXML(xml);
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                    Console.Write(row[col.ColumnName] + " | ");

                Console.WriteLine();
            }
Despertar
  • 21,627
  • 11
  • 81
  • 79