Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadXML data and Schema in DataTable

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?

like image 866
Rajkumar Vasan Avatar asked Sep 05 '25 03:09

Rajkumar Vasan


1 Answers

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.

like image 106
JamieSee Avatar answered Sep 07 '25 22:09

JamieSee