Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Reading XML documents into a Dataset


Message #1 by "uday shetgeri" <ushetgeri@y...> on Thu, 30 Jan 2003 18:15:26
> I am trying to read an XML document into a dataset and then display the 
v> alues in a datagrid in a webform.

> Here is the code snippet that I used with VB.Net/VS.Net

>         myDataset = New DataSet()
 >        myDataset.ReadXmlSchema(Server.MapPath("AuthorSchema.xsd"))
 >        myDataset.ReadXml(Server.MapPath("Authors.xml"))
 >        DataGrid1.DataSource = myDataset.Tables("authors").DefaultView
 >        DataGrid1.DataBind()

> All I get is the column headers that are the tags in the XML document.

> The problem could be the table "authors" in the dataset that I am 
trying 
t> o use while I am assigning the datasource to the datagrid. Typically 
w> hen one reads from an SQL adapter one gives a name to the table when 
f> illing the dataset. Is there a way to assign the name to the table 
when 
I>  call the ReadXml method.

> Both Authors.Xml and AuthorSchema.xsd exist and the document is valid 
as 
p> er the schema.

> Thanks in advance for any help.

> regards

Here is an update..
I can read and display the xml document if I do not read the schema 
file.. meaning it infers the schema from the document itself..

The schema looks OK.. I am pasting both the schema and document for your 
reference

Thanks

Schema file AuthorSchema.xsd
============================
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="AuthorSchema" 
targetNamespace="http://tempuri.org/AuthorSchema.xsd" 
elementFormDefault="qualified" 
xmlns="http://tempuri.org/AuthorSchema.xsd" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-
microsoft-com:xml-msdata">
	<xsd:element name="AuthorSchema" msdata:IsDataSet="true">
		<xsd:complexType>
			<xsd:choice maxOccurs="unbounded">
				<xsd:element name="authors">
					<xsd:complexType>
						<xsd:all>
							<xsd:element 
name="au_id" type="xsd:string" />
							<xsd:element 
name="au_lname" type="xsd:string" />
							<xsd:element 
name="au_fname" type="xsd:string" />
							<xsd:element 
name="phone" type="xsd:string" />
							<xsd:element 
name="address" minOccurs="0" type="xsd:string" />
							<xsd:element 
name="city" minOccurs="0" type="xsd:string" />
							<xsd:element 
name="state" minOccurs="0" type="xsd:string" />
							<xsd:element 
name="zip" minOccurs="0" type="xsd:string" />
							<xsd:element 
name="contract" type="xsd:boolean" />
						</xsd:all>
					</xsd:complexType>
				</xsd:element>
			</xsd:choice>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>

XML document : Authors.xml
==========================
<?xml version="1.0" encoding="utf-8"?> 
<NewDataSet>
  <authors>
    <au_id>409-56-7008</au_id>
    <au_lname>Bennet</au_lname>
    <au_fname>Abraham</au_fname>
    <phone>415 658-9932</phone>
    <address>6223 Bateman St.</address>
    <city>Berkeley</city>
    <state>CA</state>
    <zip>94705</zip>
    <contract>True</contract>
  </authors>
  <authors>
    <au_id>648-92-1872</au_id>
    <au_lname>Blotchet-Halls</au_lname>
    <au_fname>Reginald</au_fname>
    <phone>503 745-6402</phone>
    <address>55 Hillsdale Bl.</address>
    <city>Corvallis</city>
    <state>OR</state>
    <zip>97330</zip>
    <contract>True</contract>
  </authors>
  <authors>
    <au_id>238-95-7766</au_id>
    <au_lname>Carson</au_lname>
    <au_fname>Cheryl</au_fname>
    <phone>415 548-7723</phone>
    <address>589 Darwin Ln.</address>
    <city>Berkeley</city>
    <state>CA</state>
    <zip>94705</zip>
    <contract>True</contract>
  </authors>
</NewDataSet>

  Return to Index