Reading XML Fragments into ADO.NET DataSets

The DataSet.DataSetName is a cool property that is easily overlooked. Here is an example of where it comes in handy.

Let’s say you want to load an XML fragment into a DataSet that was retrieved via SQL Server’s FOR XML clause. You might try doing the following:

<FIELDSET>XmlReader oMyXmlReader = oCmd.ExecuteXmlReader();
oDs.ReadXml(oMyXmlReader, XmlReadMode.Fragment);

The problem with this code is that when you read the XML fragment the ReadXml method tries to match it against a schema. If it does not find a schema, then it ignores the XML. Your end result might be an empty XML document. One way to solve this is to add a schema to the DataSet. Here is one solution:

<FIELDSET>XmlReader oMyXmlReader = oCmd.ExecuteXmlReader();
oDs.ReadXmlSchema(oMyXmlReader );
oDs.ReadXml(oMyXmlReader , XmlReadMode.Fragment);
oDs.DataSetName = “myRoot”;

Now the schema exists (it was inferred) and all of the fragments can be loaded. If I want to write the XML out to a stream, file or string I might want to wrap the XML fragment inside of a parent node (like “myRoot”) so the XML is a well formatted XML document. To do this, you can use the oft overlooked property DataSetName.

Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders