How to load XML in a dataset
This example illustrates how to use XML data to load a dataset (Dataset ). This example is based on how the subject creates a dataset ing from the XSD architecture by loading the XML data to the xmldatadocument and then accessing the data from the dataset. To create an internal ing, the dataset has a schema loaded. The following example shows the conversion between the XML data and the created relational object to access the XML data.
|
VB loaddatasetxmldata. aspx |
[Running example] | [View Source Code] |
As shown in the following code, this example first implements the parseschema function to load the XML schema definition (XSD) Language schema books. XSD to the dataset attribute of xmldatadocument. In this example, the XML file books. XML is loaded using the xmldatadocument load method.
private const String document = "books.xml"; private const String myLoadSchema = "books.xsd"; private XmlDataDocument myXmlDataDocument; public static void Main() { String[] args = {document, myLoadSchema}; LoadDataSetXMLDataSample myLoadDataSetXMLDataSample = new LoadDataSetXMLDataSample(); myLoadDataSetXMLDataSample.Run(args); } public void Run(String[] args) { try { Console.WriteLine("Creating an XmlDataDocument ..."); myXmlDataDocument = new XmlDataDocument(); ParseSchema(args[1]); DisplayTableStructure(); myXmlDataDocument.Load(args[0]); DisplayTables(myXmlDataDocument.DataSet); } catch (Exception e) { Console.WriteLine ("Exception: {0}", e.ToString()); } } // Loads a specified schema into the DataSet public void ParseSchema(String schema) { StreamReader myStreamReader = null; try { Console.WriteLine("Reading Schema file ..."); myStreamReader = new StreamReader(schema); myXmlDataDocument.DataSet.ReadXmlSchema(myStreamReader); } catch (Exception e) { Console.WriteLine ("Exception: {0}", e.ToString()); } finally { if (myStreamReader != null) myStreamReader.Close(); } } private const document as string = "books.xml" private const myLoadSchema as string = "books.xsd" private myXmlDataDocument as XmlDataDocument shared sub Main() Dim args As String() = {document, myLoadSchema} Dim myLoadDataSetXMLDataSample as LoadDataSetXMLDataSample myLoadDataSetXMLDataSample = new LoadDataSetXMLDataSample() myLoadDataSetXMLDataSample.Run(args) end sub public sub Run(args As String()) try Console.WriteLine("Creating an XmlDataDocument ...") myXmlDataDocument = new XmlDataDocument() ParseSchema(args(1)) DisplayTableStructure() myXmlDataDocument.Load(args(0)) DisplayTables(myXmlDataDocument.DataSet) catch e as exception Console.WriteLine ("Exception: " & e.ToString()) end try end sub ' Loads a specified schema into the DataSet public sub ParseSchema(schema as string) Dim myStreamReader as StreamReader = nothing try myStreamReader = new StreamReader(schema) Console.WriteLine("Reading Schema file ...") myXmlDataDocument.DataSet.ReadXmlSchema(myStreamReader) catch e as exception Console.WriteLine ("Exception: " & e.ToString()) finally If Not myStreamReader Is nothing myStreamReader.Close() end if end try end sub |
C # |
VB |
|
As described in how to create a dataset ing from the XSD architecture, you only need to iterate on the set of tables, columns, and rows, and then set the output format, displaytablestructure method (using books. generate the XSD Schema file) to display the internal table structure in this example. In this example, the displaytables method is used to extend the concept (as shown in the following code). This method enables the example to display the content of an XML file. This example uses the for each keyword instead of the for loop to describe other mechanisms for repeating the set.
// Displays the contents of the DataSet tables private void DisplayTables(DataSet dataset) { // Navigate Dataset Console.WriteLine("Content of Tables ...\r\n"); foreach(DataTable table in dataset.Tables) { Console.WriteLine("TableName = " + table.TableName); Console.WriteLine ("{0}", "---------"); Console.WriteLine("Columns ...\r\n"); foreach(DataColumn column in table.Columns) { Console.Write("{0,-22}",column.ColumnName); } Console.WriteLine(); Console.WriteLine("\r\nNumber of rows = {0}", table.Rows.Count.ToString()); Console.WriteLine("Rows ...\r\n"); foreach(DataRow row in table.Rows) { foreach(Object value in row.ItemArray) { Console.Write("{0,-22}",value.ToString()); } Console.WriteLine(); } Console.WriteLine(); } } ' Displays the contents of the DataSet tables private sub DisplayTables(myDataset as DataSet) ' Navigate Dataset Console.WriteLine() Console.WriteLine("Content of Tables ...") Dim table as DataTable for each table in myDataset.Tables Console.WriteLine("TableName = " & table.TableName.ToString()) Console.WriteLine ("---------") Console.WriteLine("Columns ...") Dim column as DataColumn for each column in table.Columns Console.Write("{0,-22}",column.ColumnName.ToString()) next Console.WriteLine() Console.WriteLine("Number of rows = {0}", table.Rows.Count.ToString()) Console.WriteLine("Rows ...") Dim row as DataRow for each row in table.Rows Dim value as object for each value in row.ItemArray Console.Write("{0,-22}",value.ToString()) Next Console.WriteLine() Next Console.WriteLine() Next end sub |
C # |
VB |
|
The following output displays the table name, column name, and row content of books. XML, as shown in the displaytables method.
Creating an XmlDataDocument ...Reading Schema file ...Table structureTables count=3TableName='bookstore'.Columns count=1ColumnName='bookstore_Id', type = System.Int32TableName='book'.Columns count=5ColumnName='title', type = System.StringColumnName='price', type = System.DecimalColumnName='genre', type = System.StringColumnName='book_Id', type = System.Int32ColumnName='bookstore_Id', type = System.Int32TableName='author'.Columns count=3ColumnName='first-name', type = System.StringColumnName='last-name', type = System.StringColumnName='book_Id', type = System.Int32Content of Tables ...TableName = bookstore---------Columns ...bookstore_IdNumber of rows = 1Rows ...0TableName = book---------Columns ...title price genre book_Id bookstore_IdNumber of rows = 3Rows ...The Autobiography of Benjamin Franklin8.99 autobiography 0 0The Confidence Man 11.99 novel 1 0The Gorgias 9.99 philosophy 2 0TableName = author---------Columns ...first-name last-name book_IdNumber of rows = 3Rows ...Benjamin Franklin 0Herman Melville 1Sidas Plato 2
Summary
- You can use the relational method on the dataset attribute to access the XML data that has been loaded into xmldatadocument.
- When you use the dataset attribute of xmldatadocument to input relational data, you can also read XML data.