Creating DataSet from XML and XSD

Level: Beginner-Intermediate

If you want to read your XML file into a DataSet, you could use the ReadXml method of the DataSet. You can read the XSD for the same XML file using ReadXMLSchema method. Here is how to use these methods,

string doc = @”c:\test.xml”;
string docSchema = @”c:\test.xsd”;
XmlDataDocument myXmlDataDocument = new XmlDataDocument ;

We will use a StreamReader to read the schema file,

StreamReader reader = new StreamReader(docSchema);
//this will read the schema to DataSet

[do not forget to close the StreammReader in your actual code]

//this will read the XML

DataSet also has a ReadXML method you can use to read the XML. This method can be used instead of XMLDataDocument’s Load method. ReadXML method takes a second parameter that specifies XMLReadMode [enumeration].
Read more about this on MSDN:

All these methods works well with simple XML and XSD. In my experience, if you have many complex types and relations in XML/XSD, ReadXMLSchema method gives undefined error! on some types [native and sometimes derived!] and subsequently Load fails!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s